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


Messages In This Thread
sleep(n) not working inside tkinter mainloop - by roger31415 - Jul-14-2019, 01:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,085 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,565 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,596 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  tkinter toggle buttons not working Nu2Python 26 7,043 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,658 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  TKinter restarting the mainloop when button pressed zazas321 7 16,431 Jan-26-2021, 06:38 AM
Last Post: zazas321
  python file(.py) not working with my tkinter project DeanAseraf1 9 7,195 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,495 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,779 Dec-16-2019, 04:47 AM
Last Post: woooee
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,153 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