解决python tkinter 与 sleep 延迟问题

用户7886150 2021-01-22 14:53:11
Python tkinter 解决 延迟 sleep


参考链接: Python sleep()

多线程(threading——join) 

 join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等待,直到子线程B完成操作后, 

 才可以接着往下执行,那么在调用这个线程时可以使用被调用线程的join方法。join([timeout]) 里面的参数时可选的,代表线程运行的最大时 

 间,即如果超过这个时间,不管这个此线程有没有执行完毕都会被回收,然后主线程或函数都会接着执行的,如果线程执行时间小于参数表示的 

 时间,则接着执行,不用一定要等待到参数表示的时间。 

   1 import threading

 2 import time

 3 #创建两个测试的变量

 4 num_1 = 0

 5 num_2 = 0

 6 def te_1():

 7     global num_1

 8     #将num_1加到 3

 9     for i in range(3):

10         num_1 += 1

11         print("我是num_1: " + str(num_1))

12         time.sleep(1)

13 def te_2():

14     global num_2

15     #将num_2加到 3

16     for i in range(3):

17         num_2 += 1

18         print("我是num_2: " + str(num_2))

19         time.sleep(1)

20 t1 = threading.Thread(target=te_1)

21 t1.start()

22 t2 = threading.Thread(target=te_2)

23 #join为等待线程,等待t1线程执行完,再执行t2线程

24 t1.join()

25 t2.start() 

 多线程(threading——setDeamon) 

 setDaemon()方法。主线程A中,创建了子线程B,并且在主线程A中调用了B.setDaemon(),这个的意思是,把主线程A设置为守护线程,这 

 时候,要是主线程A执行结束了,就不管子线程B是否完成,一并和主线程A退出.这就是setDaemon方法的含义,这基本和join是相反的。此外,还有 

 个要特别注意的:必须在start() 方法调用之前设置,如果不设置为守护线程,程序会被无限挂起,只有等待了所有线程结束它才结束。 

   1 import threading

 2 import time

 3 #创建两个测试的变量

 4 num_1 = 0

 5 num_2 = 0

 6 def te_1():

 7     global num_1

 8     #将num_1加到 3

 9     for i in range(3):

10         num_1 += 1

11         print("我是num_1: " + str(num_1))

12         time.sleep(1)

13 def te_2():

14     global num_2

15     #将num_2加到 5

16     for i in range(5):

17         num_2 += 1

18         print("我是num_2: " + str(num_2))

19         time.sleep(1)

20 t1 = threading.Thread(target=te_1)

21 t1.start()

22 t2 = threading.Thread(target=te_2)

23 #守护进程t2,当t1执行完后不再去执行t2

24 t2.setDaemon(t1)

25 t2.start() 

 下面是如何解决time.sleep,其实非常简单,只需要创建一个线程让他自己执行就可以了 

   1 """小白随笔,大佬勿喷"""

 2 '''tkinter解决time延迟问题'''

 3 import tkinter as tk

 4 import time

 5 import threading

 6 #初始化窗口

 7 window = tk.Tk()

 8 #窗口名称

 9 window.title("My Window")

10 #窗口大小,是 x 不是 *

11 window.geometry("400x400")

12 #不能改变窗口的大小

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             #获取当前时间

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

22             label.config(text=date)

23             #更新窗口

24             window.update()

25             time.sleep(1)

26         except:

27             break

28 #创建按钮,只是为了测试sleep与按钮之间是否存在延迟

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

30 #创建线程,如果函数里面有参数,args=()

31 t = threading.Thread(target=count)

32 #开启线程

33 t.start()

34 #循环窗口

35 window.mainloop() 

 技术有限,不会录制gif图 

转载于:https://www.cnblogs.com/py-peng/p/10339597.html

原文链接: 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

版权声明
本文为[用户7886150]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1778424

  1. appium+python自动化63-使用Uiautomator2报错问题解决
  2. Tornado: the mainstream Python Web Framework
  3. Appium + Python automation 63 - using uiautomator2 to solve the problem of error reporting
  4. 爬虫+django,打造个性化API接口
  5. Crawler + Django to create personalized API interface
  6. 爬虫+django,打造个性化API接口
  7. Crawler + Django to create personalized API interface
  8. C、C++、Java、PHP、Python主要应用在哪里方面?
  9. C. Where are the main applications of C + +, Java, PHP and python?
  10. Python 无限级分类树状结构生成算法 「实用代码」
  11. Python infinite classification tree structure generation algorithm "practical code"
  12. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
  13. [azure storage service] Python module( azure.cosmosdb.table )Direct operation example of storage account table
  14. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例
  15. [azure storage service] Python module( azure.cosmosdb.table )Direct operation example of storage account table
  16. openpose c++ 配置教程 + python api
  17. Openpose C + + configuration tutorial + Python API
  18. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
  19. 使用python javaSerializationTools模块拼接生成 8u20 Gadget
  20. 萌新入门之python基础语法
  21. python中hmac模块的使用
  22. Python crawler_ Garbage man idle fish crawler turn crawler data integration self use second hand rapid response garbage collection platform_ 3 (with continuous source update)
  23. Using Python javaserialization tools module to generate 8u20 gadget
  24. The basic syntax of Python
  25. The use of HMAC module in Python
  26. 攻防世界web进阶区Web_python_block_chain详解
  27. Attack and defense world web advanced zone Web_ python_ block_ Details of chain
  28. pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作
  29. Add rows and columns, modify, delete, filter, judge elements and transpose operations in pandas dataframe
  30. pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作
  31. Add rows and columns, modify, delete, filter, judge elements and transpose operations in pandas dataframe
  32. 虚言妙诀终虚见,面试躬行是致知,Python技术面试策略与技巧实战记录
  33. The interview practice is knowledge, python technology interview strategy and skills of the actual record
  34. 用tqdm和rich为固定路径和目标的python算法代码实现进度条
  35. Using tqdm and rich as the fixed path and target of Python algorithm code to realize the progress bar
  36. 我来记笔记啦-Django开发流程与配置
  37. Let me take notes - Django development process and configuration
  38. python数据类型的强制转换
  39. Django报错:'Key 'id' not found in 'xxx'. Choices are: xxx'
  40. Python400集大型视频,从正确的方向出发学习,全套完整送给大家
  41. Mandatory conversion of Python data type
  42. Django reported an error: 'key' ID 'not found in' xxx '. Choices are: xxx'
  43. Python 400 sets of large video, starting from the right direction to learn, a complete set to you
  44. 只需十四步:从零开始掌握Python机器学习(附资源)
  45. Just 14 steps: Master Python machine learning from scratch (resources attached)
  46. Python|文件读写
  47. 安利一个Python界神奇得网站
  48. Python | file reading and writing
  49. Amway is a marvelous website in Python world
  50. 第二热门语言:从入门到精通,Python数据科学简洁教程
  51. The second popular language: from introduction to mastery, python data science concise tutorial
  52. 以我的亲身经历,聊聊学python的流程,同时推荐学python的书
  53. With my own experience, I'd like to talk about the process of learning Python and recommend books for learning python
  54. 以我的亲身经历,聊聊学python的流程,同时推荐学python的书
  55. With my own experience, I'd like to talk about the process of learning Python and recommend books for learning python
  56. Django url 路由匹配过程
  57. Django URL routing matching process
  58. 强者一出,谁与争锋?与Python相比,C++的运行速度究竟有多快?
  59. Who will fight against the strong? How fast is C + + running compared with Python?
  60. python 学习体会