当前位置:网站首页>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
边栏推荐
- Getting started with webgl (1)
- Pit avoidance: description of null values in in and not in SQL
- 简述keepalived工作原理
- Oracle控制文件丢失恢复归档模式方法
- [quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
- 微信小程序 01
- 什么是pv和uv? pv、uv
- 【深度学习】语义分割实验:Unet网络/MSRC2数据集
- 什么是数据泄露
- 【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
猜你喜欢
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
The difference between full-time graduate students and part-time graduate students!
Typescript release 4.8 beta
Introduction of mongod management database method
Qu'est - ce qu'une violation de données
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
【深度学习】图像超分实验:SRCNN/FSRCNN
Ctfshow, information collection: web5
How to create Apple Developer personal account P8 certificate
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
随机推荐
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
Wechat applet 01
最安全的证券交易app都有哪些
Getting started with webgl (3)
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Gd32 F3 pin mapping problem SW interface cannot be burned
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
【OBS】RTMPSockBuf_Fill, remote host closed connection.
摘抄的只言片语
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
Webgl texture
Window环境下配置Mongodb数据库
OpenGL common functions
什麼是數據泄露
The rebound problem of using Scrollview in cocos Creator
Qu'est - ce qu'une violation de données
Detailed explanation of Cocos creator 2.4.0 rendering process
Introduction of mongod management database method