Bottom Page

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()
Quote
#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
Quote
#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 :-)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Is mainloop() necessary? escape_freedom13 2 209 Sep-01-2019, 02:49 PM
Last Post: escape_freedom13
  python file(.py) not working with my tkinter project DeanAseraf1 8 259 Jul-30-2019, 12:55 PM
Last Post: DeanAseraf1
  [split] What is a GUI's mainloop ? kom2 1 319 May-04-2019, 01:58 PM
Last Post: Yoriz
  [Tkinter] ttk.barprogress - root.mainloop francisco_neves2020 11 895 Apr-10-2019, 01:06 PM
Last Post: francisco_neves2020
  tkinter - global variable not working albry 2 1,034 Jan-26-2019, 04:22 AM
Last Post: Larz60+
  Tkinter, button doesnt working ;// Dzonzi 3 2,041 Jul-18-2018, 11:14 AM
Last Post: buran
  [Tkinter] How to update the gui in a loop without blocking the gui mainloop Qnuba 2 30,365 Nov-12-2016, 04:33 PM
Last Post: Qnuba

Forum Jump:


Users browsing this thread: 1 Guest(s)