0

I'm a beginner and want to make a fast reading application with tkinter library. The app consists of a Start Button and speed Entry to show the user text in the Message widget. when Start Button press, an Event handler should run and show words based on given speed on the Message widget but the Message widget not changed until the event handler exit. for e.g. in following code the Message widget not changed until the do_something function end.

import tkinter as tk
import time

def do_something():
    msg.configure(text = "changing")
    for i in range(20):
        time.sleep(1)
        print("...")
    print("exit of loop")
    
window = tk.Tk()
window.geometry("8x50")
window.rowconfigure([1,2], weight = 1, minsize = 10)
window.columnconfigure(1, weight = 1, minsize = 10)
global msg
msg = tk.Message(window, bg = 'grey' ,text = 'Hello', width = 50 )
btn = tk.Button(window, text='click me', width = 10, height = 1, command = do_something)

msg.grid(row = 1, column = 1, sticky = 'nswe')
btn.grid(row = 2, column = 1, sticky = 'nswe')

window.mainloop()

Now how can I justify this code to make the change immediately on widgets before event handler ending? Excuse me for my poor english

Hoo Loo
  • 23
  • 5
  • Using `sleep` is freezing the UI. You need to replace `sleep` with `after`. Search for tkinter, sleep and after for examples. `after` will need your amended `do_something` function as it's callback. – Tls Chris Nov 27 '20 at 18:25
  • You have to use `window.update()` to make changes in a specific point of your code, otherwise it will only update when it reaches the `window.mainloop()` again – Flavio Moraes Nov 27 '20 at 19:35

0 Answers0