当前位置:网站首页>Tkinter after how to refresh data and cancel refreshing
Tkinter after how to refresh data and cancel refreshing
2022-07-07 15:40:00 【Mh_ Python_ learn】
tkinter after How to refresh data and cancel refresh
stay tkinter In software design , We often need to constantly refresh the data we want , Let's take the tag timer as an example to explain tkinter How to refresh data
import tkinter as t
class mian():
def __init__(self,a):
self.wind=a
self.wind.geometry("800x600")
self.bk = t.Canvas(wind, width=800, height=600, bg="lightyellow")
self.bk.place(x=0, y=0)
self.btn = t.Button(self.bk, text=" Start timing ", bg="yellow", font="Helvetic 20 bold", width=10, command=self.djs)
self.btn.place(x=20, y=180)
self.btn1 = t.Button(self.bk, text=" Cancel the timing ", bg="yellow", font="Helvetic 20 bold", width=10, command=self.end)
self.btn1.place(x=20, y=280)
self.num = 10
self.num_lab = t.Label(self.bk, text=self.num, bg="yellow", font="Helvetic 20 bold", width=10)
self.num_lab.place(x=400, y=180)
def djs(self):
pass
def end(self):
pass
wind=t.Tk()
mian(wind)
wind.mainloop()
Let's first create a window interface , At this time, the start timing button and the end time button are not added
We add a timing program at this time after
def djs(self):
self.num_lab.place_forget()
self.num_lab = t.Label(self.bk, text=self.num, bg="yellow", font="Helvetic 20 bold", width=10)
self.num_lab.place(x=400, y=180)
self.s=self.bk.after(1000,self.jian)
def jian(self):
self.num-=1
self.djs()
Sub function function function adding code is shown , It's essentially a function djs and jian Jump every second between , What needs to be noted here is , If you click start again in time , Our timer will restart again , Because the whole software is a message cycle , Every time you click the button , Our this self.s The action of the timer starts once , You will find that the countdown is no longer 1s Once , But faster and faster , Cancel the timer , We can solve this problem
def end(self):
self.bk.after_cancel(self.s)
self.num=10
Custom function end Connected is our cancel timer button , When we click Cancel timing , set out after_cancel Instructions , Cancel the timer .
The complete code is as follows :
import tkinter as t
class mian():
def __init__(self,a):
self.wind=a
self.wind.geometry("800x600")
self.bk = t.Canvas(wind, width=800, height=600, bg="lightyellow")
self.bk.place(x=0, y=0)
self.btn = t.Button(self.bk, text=" Start timing ", bg="yellow", font="Helvetic 20 bold", width=10, command=self.djs)
self.btn.place(x=20, y=180)
self.btn1 = t.Button(self.bk, text=" Cancel the timing ", bg="yellow", font="Helvetic 20 bold", width=10, command=self.end)
self.btn1.place(x=20, y=280)
self.num = 10
self.num_lab = t.Label(self.bk, text=self.num, bg="yellow", font="Helvetic 20 bold", width=10)
self.num_lab.place(x=400, y=180)
def djs(self):
self.num_lab.place_forget()
self.num_lab = t.Label(self.bk, text=self.num, bg="yellow", font="Helvetic 20 bold", width=10)
self.num_lab.place(x=400, y=180)
self.s=self.bk.after(1000,self.jian)
def jian(self):
self.num-=1
self.djs()
def end(self):
self.bk.after_cancel(self.s)
self.num=10
wind=t.Tk()
mian(wind)
wind.mainloop()
That's the question , If we repeatedly click the start timer button, it will still appear that we have started multiple timers, resulting in faster and faster countdown , How should we solve it ?
Welcome to share your thoughts
边栏推荐
- Nacos一致性协议 CP/AP/JRaft/Distro协议
- Pit avoidance: description of null values in in and not in SQL
- Getting started with webgl (2)
- [quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
- Steps to create P8 certificate and warehousing account
- 大表delete删数据导致数据库异常解决
- Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
- Yunxiaoduo software internal test distribution test platform description document
- Oracle control file loss recovery archive mode method
- 【数字IC验证快速入门】26、SystemVerilog项目实践之AHB-SRAMC(6)(APB协议基本要点)
猜你喜欢
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
Win10 or win11 taskbar, automatically hidden and transparent
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
15. Using the text editing tool VIM
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
什么是数据泄露
With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
Bye, Dachang! I'm going to the factory today
随机推荐
Matlab experience summary
什麼是數據泄露
How to understand that binary complement represents negative numbers
The difference between full-time graduate students and part-time graduate students!
Window环境下配置Mongodb数据库
What are the safest securities trading apps
众昂矿业:萤石继续引领新能源市场增长
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
最安全的证券交易app都有哪些
jacoco代码覆盖率
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
Gd32 F3 pin mapping problem SW interface cannot be burned
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
[target detection] yolov5 Runtong voc2007 data set
Excerpted words
什么是pv和uv? pv、uv
15. Using the text editing tool VIM
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Qu'est - ce qu'une violation de données
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)