Tuesday, May 22, 2012

Python: How to make a clock in tkinter using time.strftime

This is a tutorial on how to create a clock / Timer using tkinter in Python, note you might have to change Tkinter to tkinter depending on your version of the Python you have. I am using Python 3.2 at the moment. The code itself is quite simple, the only part you need to know is how to get the current time, using time.strftime. You can also get the day of the week, the current month, etc, by changing the parameter you are supplying the function. (See the link for the full table of possible options). But yes, almost anything you can think of any use of, it is there already. :) So this can actual be developed to a calendar like GUI.

Related Tutorial with source code
Stop watch GUI (for counting time): http://ygchan.blogspot.com/2012/05/python-stop-watch-timer-source-code.html


import Tkinter as tk
import time

def update_timeText():
    # Get the current time, note you can change the format as you wish
    current = time.strftime("%H:%M:%S")
    # Update the timeText Label box with the current time
    timeText.configure(text=current)
    # Call the update_timeText() function after 1 second
    root.after(1000, update_timeText)

root = tk.Tk()
root.wm_title("Simple Clock Example")

# Create a timeText Label (a text box)
timeText = tk.Label(root, text="", font=("Helvetica", 150))
timeText.pack()
update_timeText()
root.mainloop()
Reference: http://docs.python.org/library/time.html#time.strftime
Reference: http://docs.python.org/library/string.html#formatstrings

3 comments:

  1. Can you explain how to add a time widget where u ask user to set a time . Basically grab a input from user where u can check for the system time and user input time to do a action..

    ReplyDelete
    Replies
    1. Try to see if this work, it is sort of a hack.

      # For Python 2.x use "Tkinter"
      # import Tkinter as tk

      # For Python 3.x use "tkinter"
      import tkinter as tk
      import datetime

      # Get the current time
      myTime = datetime.datetime.now()

      # Note it does not check if the time is valid!
      def getText():
      global myTime
      myString = inputBox.get()

      # Did not validate all the cases for the input
      # The time needs to be correct to work
      if (len(myString) == 6):
      # Remove the message and the buttons
      simpleTitle.pack_forget()
      inputBox.pack_forget()
      button.pack_forget()

      # Setup the new time to the user defined time
      hour = int(myString[0:2])
      minute = int(myString[2:4])
      second = int(myString[4:6])
      myTime = datetime.datetime(2000, 1, 1, hour, minute, second)

      def update_timeText():
      global myTime
      # Increment the time by 1 second every frame
      myTime += datetime.timedelta(seconds=1)
      current = myTime.strftime("%H:%M:%S")
      # Update the timeText Label box with the current time
      timeText.configure(text=current)
      # Call the update_timeText() function after 1 second
      root.after(1000, update_timeText)

      root = tk.Tk()
      root.wm_title("Simple Clock Example")

      # The message title
      simpleTitle =tk.Label(root)
      simpleTitle['text'] = "Please Enter the time:\
      \nExample: 06:30:00am, Enter 063000\
      \nExample: 08:30:00pm, Enter 203000"
      simpleTitle.pack()

      # The entry box widget
      inputBox = tk.Entry(root)
      inputBox.pack()

      # The submit box widget
      button = tk.Button(root, text='Submit', command=getText)
      button.pack()

      # Create a timeText Label (a text box)
      timeText = tk.Label(root, text="", font=("Helvetica", 150))
      timeText.pack()
      update_timeText()
      root.mainloop()

      Delete
  2. Thanks for sharing such a great blog Keep posting..
    Python Training
    Python Course

    ReplyDelete