Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] sleep(n) not working inside tkinter mainloop
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")

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

def test_function():



Please see the following forum thread for a tutorial showing how to use sleep (blocking code) with tkinter.
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
    index += 1
    if index == len(data): return
    main_win.after(delay, display_loop, data, index)

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

data = [[5000, 'A'], [5000, 'B'], [0, 'C']]
wuf :-)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] What is a GUI's mainloop ? kom2 1 166 May-04-2019, 01:58 PM
Last Post: Yoriz
  [Tkinter] ttk.barprogress - root.mainloop francisco_neves2020 11 552 Apr-10-2019, 01:06 PM
Last Post: francisco_neves2020
  tkinter - global variable not working albry 2 550 Jan-26-2019, 04:22 AM
Last Post: Larz60+
  [Tkinter] Treeview automatically adjust it's size when pack inside frame Prince_Bhatia 1 3,554 Jul-25-2018, 03:24 AM
Last Post: Larz60+
  Tkinter, button doesnt working ;// Dzonzi 3 1,405 Jul-18-2018, 11:14 AM
Last Post: buran
  Canvas not placing inside Grid leonv 2 982 Nov-21-2017, 05:37 PM
Last Post: leonv
  Box Sizer inside of panel managed by wx.lib.agw.aui Larz60+ 1 1,421 Jun-13-2017, 07:25 AM
Last Post: Larz60+
  [Tkinter] How to update the gui in a loop without blocking the gui mainloop Qnuba 2 24,790 Nov-12-2016, 04:33 PM
Last Post: Qnuba

Forum Jump:

Users browsing this thread: 1 Guest(s)