Python Forum
[Tkinter] sleep(n) not working inside tkinter mainloop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] sleep(n) not working inside tkinter mainloop
#1
Hello and thanks in advance for any help. I've reproduced an issue present in complex code, in much simpler test code shown below that illustrates the fundamental issue. When this code executes, "A", "B", and "C" are displayed on the console from the print statements with a five second delay between A and B, and B and C, as expected. However when the test_function executes, after the total ten second delay, the tkinter window then appears with only "C" displayed, skipping the display of "A" and "B". The expectation was that the window would first appear with "A", and then after five seconds, the label would change to "B" and then after another five seconds it would change to "C".

I also get the same result if instead of calling test_function(), I replace the function call with the code itself. Also, if I invoke the window first displaying some other unrelated widget, and then try to display the label widget with the same sleep scenario, only the final label text assignment "C" is shown, and never "A" or "B".

I'm a new Python coder, and I'm sure I'm missing something very fundamental, perhaps related to blocking? The sleep delay functionality will ultimately be used (with much shorter durations) to pulse Raspberry Pi output ports when various tkinter buttons are clicked. Here's the test code, and thanks again!

from time import sleep
from tkinter import *

w = Tk()
w.title("sleep test")
w.geometry('100x100')

test_label=Label(w, text="A")
test_label.grid(column=0, row=0)

def test_function():
    test_label.config(text="A")
    sleep(5)
    test_label.config(text="B")
    sleep(5)
    test_label.config(text="C")

print("A")
sleep(5)
print("B")
sleep(5)
print("C")

test_function();

w.mainloop()
Reply
#2
Please see the following forum thread for a tutorial showing how to use sleep (blocking code) with tkinter.
https://python-forum.io/Thread-Tkinter-H...ng-the-gui
Reply
#3
Hi roger31415

Here one variant without using time.sleep:
from tkinter import *
 

def display_loop(data, index=0):
    display_item = data[index]
    delay, text = display_item
    test_label.configure(text=text)
    index += 1
    if index == len(data): return
    
    main_win.after(delay, display_loop, data, index)
    

main_win = Tk()
main_win.title("Delay Test")
main_win.geometry('100x100')
 
test_label=Label(main_win, font=('Helvetica', 20, 'bold'))
test_label.grid(column=0, row=0)

data = [[5000, 'A'], [5000, 'B'], [0, 'C']]
display_loop(data) 
 
main_win.mainloop()
wuf :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 4,858 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,479 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,448 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  tkinter toggle buttons not working Nu2Python 26 6,767 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,598 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  TKinter restarting the mainloop when button pressed zazas321 7 16,088 Jan-26-2021, 06:38 AM
Last Post: zazas321
  python file(.py) not working with my tkinter project DeanAseraf1 9 6,981 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,356 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,709 Dec-16-2019, 04:47 AM
Last Post: woooee
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,097 Oct-23-2019, 06:23 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020