## Solve the delay problem of Python Tkinter and sleep

User 7886150 2021-01-22 14:54:20
solve delay problem python tkinter

join () Method ： The main thread A in , Created a child thread B, And in the main thread A Called in B.join(), that , The main thread A It will wait where it is called , Until the child thread B After completing the operation ,

Then we can carry on , When calling this thread, you can use the join Method .join([timeout]) The parameters are optional , Represents the maximum running time of a thread

between , That is, if it exceeds this time , No matter whether the thread has finished executing or not, it will be recycled , Then the main thread or function will continue to execute , If the thread execution time is less than the parameter

Time , Then execute , You don't have to wait until the time indicated by the parameter .

2 import time

3 # Create two test variables

4 num_1 = 0

5 num_2 = 0

6 def te_1():

7     global num_1

8     # take num_1 Add to 3

9     for i in range(3):

10         num_1 += 1

11         print(" I am a num_1: " + str(num_1))

12         time.sleep(1)

13 def te_2():

14     global num_2

15     # take num_2 Add to 3

16     for i in range(3):

17         num_2 += 1

18         print(" I am a num_2: " + str(num_2))

19         time.sleep(1)

21 t1.start()

23 #join For the waiting thread , wait for t1 The thread is finished , Re execution t2 Threads

24 t1.join()

25 t2.start()

setDaemon() Method . The main thread A in , Created a child thread B, And in the main thread A Called in B.setDaemon(), This means , Put the main thread A Set to daemons , this

When , If the main thread A Execution is over , It doesn't matter if the child thread B Whether it is completed or not , Together with the main thread A sign out . This is it. setDaemon The meaning of method , It's basically the same join It's the opposite . Besides , also

One thing to pay special attention to ： Must be in start() Set before method invocation , If it is not set as a daemonic thread , The program will be suspended indefinitely , Only after waiting for all threads to end does it end .

2 import time

3 # Create two test variables

4 num_1 = 0

5 num_2 = 0

6 def te_1():

7     global num_1

8     # take num_1 Add to 3

9     for i in range(3):

10         num_1 += 1

11         print(" I am a num_1: " + str(num_1))

12         time.sleep(1)

13 def te_2():

14     global num_2

15     # take num_2 Add to 5

16     for i in range(5):

17         num_2 += 1

18         print(" I am a num_2: " + str(num_2))

19         time.sleep(1)

21 t1.start()

23 # Daemon t2, When t1 No more execution after execution t2

24 t2.setDaemon(t1)

25 t2.start()

Here's how to solve time.sleep, It's very simple , Just create a thread and let it execute by itself

1 """ Xiaobai's essay , Bosses do not spray """

2 '''tkinter solve time Delay problem '''

3 import tkinter as tk

4 import time

6 # Initialize window

7 window = tk.Tk()

8 # Window name

9 window.title("My Window")

10 # Window size , yes x No *

11 window.geometry("400x400")

12 # You can't change the size of the window

13 window.resizable(width=False,height=False)

14 def count():

15     label = tk.Label(window,bg='green')

16     label.pack()

17     button.pack()

18     while True:

19         try:

20             # Get the current time

21             date = time.strftime("%Y-%m-%d\n%H:%M:%S")

22             label.config(text=date)

23             # Update window

24             window.update()

25             time.sleep(1)

26         except:

27             break

28 # Create button , Just for testing sleep Whether there is a delay between and the button

29 button = tk.Button(window,text='Hit_me',width=20)

30 # Create thread , If there are arguments in a function ,args=()

33 t.start()

34 # Loop window

35 window.mainloop()

Limited technology , I can't record gif chart

Reproduced in :https://www.cnblogs.com/py-peng/p/10339597.html

Link to the original text ： https://blog.csdn.net/weixin_30951743/article/details/96667804?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161129610916780255221056%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161129610916780255221056&biz_id=0&utm_medium=distribute.pc_search_result

https://pythonmana.com/2021/01/20210122145245425m.html