hey everyone, i am writting an app with tkinter, well ive tried quite alot of different ways but i couldnt find the solution for my problem. So, in that window there is time and i need it to constantly change, like every second. Any ideas? Thanks Dan08

hey everyone, i am writting an app with tkinter, well ive tried quite alot of different ways but i couldnt find the solution for my problem. So, in that window there is time and i need it to constantly change, like every second. Any ideas? Thanks Dan08

hi,
could you be more specific about what you're trying to do, e.g. provide your code so far, or some example, cause it's not very clear :)
Are you trying to make a clock or something?

P.S sorry if I'm missing something

The code of the whole program is enormous, so i just made it smaller:

from Tkinter import *
import time
Label(Tk(), text=time.strftime("%d/%m/%Y %A %H:%M:%S")).pack()
Tk().mainloop()

Now when i run this, i need to see the time changing. Any ideas, thanks dan08.

that's much clearer :) thanks

here's one way of doing this:

from Tkinter import *
import time

root = Tk()
l = Label( root, text=time.strftime( "%d/%m/%Y %A %H:%M:%S") )
l.pack()
root.update()

while True:
    time.sleep( 1 )
    l[ "text" ]=time.strftime( "%d/%m/%Y %A %H:%M:%S" )
    root.update()
    
mainloop()

of course you could make this into a separate thread as well :)

Edited 6 Years Ago by masterofpuppets: n/a

Isn't there anyother way coz when i try the masterofpuppets' code it keeps giving me this error:

Traceback (most recent call last):
  File "C:\Users\Dan08\Desktop\program.py", line 11, in <module>
    l[ "text" ]=time.strftime( "%d/%m/%Y %A %H:%M:%S" )
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1209, in __setitem__
    self.configure({key: value})
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1202, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1193, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: invalid command name ".53252544"

It works, but when i run other parts of the program dont work, like the title and so on. Any ideas, Dan08.

For timed events you generally use the "after" function.

from Tkinter import *
import datetime
import time

class ChangeTime(Frame):

    def __init__(self, master=None):
        master.geometry("100x50+5+5")
        Frame.__init__(self, master)
        self.pack()

        self.timestr = StringVar()
        lbl = Label(master, textvariable=self.timestr)
        lbl.pack()

        # register callback
        self.listenID = self.after(1000, self.newtime)

    def newtime(self):
        timenow = datetime.datetime.now()
        self.timestr.set("%d:%02d:%02d" % \  
                 (timenow.hour, timenow.minute, timenow.second))
        self.listenID = self.after(1000, self.newtime)


root=Tk()
CT = ChangeTime(root)
CT.mainloop()

I dont really know how to put it working, any help please!! woooee's method is working but i dont know how to put his code into the massive lines of code. Dan08

This article has been dead for over six months. Start a new discussion instead.