Page 1 of 1

Dynamically Updating Time in Tkinter

Posted: Wed Jan 02, 2019 5:31 pm
by Myles9
Hi,

I am trying to make a window (with Tkinter) with a label that dynamically updates the time every minute.

Here is my code:

Code: Select all

import Tkinter as tk
import time

def update_time():
        time_string = time.strftime('%I:%M %p')
        self.message.config = "The current time is " + time_string)
        root.after(60000, update_time)

        
root = tk.Tk()

root.attributes('-fullscreen', True)


message = tk.Label(root,
                 text="The current time is " + time_string,
                 font = "Helvetica 30").pack()

update_time()

root.mainloop()
This just causes my GUI to crash. It runs on startup, so I can't extract any errors.
When I try to run it by itself, the Python crash handler says that there is no module named Tkinter.

I am using Python 2.

When I run the code below, it works fine, but doesn't update the time:

Code: Select all

import Tkinter as tk
import time

def update_time():
        time_string = time.strftime('%I:%M %p')
        #self.message.config = "The current time is " + time_string)
        root.after(60000, update_time)

        
root = tk.Tk()

root.attributes('-fullscreen', True)


message = tk.Label(root,
                 text="The current time is " + time_string,
                 font = "Helvetica 30").pack()

update_time()

root.mainloop()

Could anyone tell me what I'm doing wrong? :?

Re: Dynamically Updating Time in Tkinter

Posted: Thu Jan 03, 2019 9:56 am
by scotty101
Any error messages?

This one perhaps?

Code: Select all

Traceback (most recent call last):
  File "../time_gui.py", line 17, in <module>
    text="The current time is " + time_string,
NameError: name 'time_string' is not defined

Re: Dynamically Updating Time in Tkinter

Posted: Thu Jan 03, 2019 10:05 am
by scotty101
There were a number of problems. I've put some comments in the updated code to show you what I've changed. You've made the standard noob mistake of creating a label and using pack/grid on the same line. Never do this. It only causes pain and isn't worth the few seconds it saves.

I did modify you code to update every second and to show the seconds of the time too. This just made it easier for me to test. Saved me waiting 60s for it to update.

Code: Select all

#https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=230280
import Tkinter as tk
import time

#You need to define time string so that the label display it.
time_string = time.strftime('%I:%M %p')

def update_time():
    #Made time_string global
    global time_string
    time_string = time.strftime('%I:%M:%S %p')
    #Use the config method of message to update the text
    message.config(text= "The current time is " + time_string)
    root.after(1000, update_time)

        
root = tk.Tk()

#root.attributes('-fullscreen', True)


message = tk.Label(root,
                 text="The current time is " + time_string,
                 font = "Helvetica 30")
#Changed - NEVER create and layout the widgets on the same line of code
message.pack()

update_time()

root.mainloop()
Also I notice by the fact that you are using "Tkinter" rather than "tkinter" that you are still using python2. Unless you have a really good reason, you should write all new python code with python 3.

Re: Dynamically Updating Time in Tkinter

Posted: Thu Jan 03, 2019 10:26 pm
by Myles9
OK, thanks!

As far as I can tell, that worked perfectly!
The reason that I am using Python 2 is that I have to use some other libraries that are specific to Python 2.

Thanks again!