Skip to content Skip to sidebar Skip to footer

Tkinter: Analog Clock Refreshing - How Does "after" Function Work?

Recently, I'm beginner in python, and I've programmed an analog clock on Tkinter, which can display time every second. But this clock uses a while 1 infinite loop to refresh the ti

Solution 1:

The problem is that in the principle function, HORLOGE1() calls itself immediately while trying to arrange for it to be called again after the specified 1 second delay. This occurs in the last line:

fen1.after(1000, HORLOGE1(250, 250, 200))

A simple way prevent this, would be to use something like this instead:

fen1.after(1000, lambda: HORLOGE1(250, 250, 200))

What this does is create an anonymous function using a lambda expression . The function created accepts/requires no arguments, and all it does is call HORLOGE1(250, 250, 200) when it's called. That function is what is then passed to fen1.after() as the second argument.

The notable thing is that doing this does not actually call HORLOGE1() in the process. The second argument to .after() should almost always be a callable — such as a function or a bound method — rather than the results of calling one, as you are doing.

After making the change, you should see something like this with moving clock hands on it:

screenshot of analog clock displayed in tkinter window

Post a Comment for "Tkinter: Analog Clock Refreshing - How Does "after" Function Work?"