简体   繁体   中英

Python: Does after() in Tkinter have a multi-threading approach?

I'm writing a physics simulating program, and found after() useful.

I once would like to create a thread for physics calculation and simulation. But when I finally noticed that function, I used it instead.

So, I'm curious about how Tkinter implements that function. Is it multi-threading?

It is not multithreaded.

Tkinter works by pulling objects off of a queue and processing them. Usually what is on this queue are events generated by the user (mouse movements, button clicks, etc).

This queue can contain other things, such as job created with after . So, to Tkinter, something submitted with after is just another event to be processed at a particular point in time.

The following script will freeze the GUI for 5 seconds so it is not multi-threaded:

from Tkinter import Tk, Label
from time import sleep
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
w.after(100, lambda: sleep(5))
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM