当前位置:网站首页>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
边栏推荐
- 什么是数据泄露
- [server data recovery] data recovery case of raid failure of a Dell server
- 【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
- Mathematical modeling -- what is mathematical modeling
- 【搞船日记】【Shapr3D的STL格式转Gcode】
- Briefly describe the working principle of kept
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- MySQL bit类型解析
- Matlab experience summary
- HPDC smart base Talent Development Summit essay
猜你喜欢
Gd32 F3 pin mapping problem SW interface cannot be burned
What is Base64?
Monthly observation of internet medical field in May 2022
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
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
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
Ctfshow, information collection: web14
Super signature principle (fully automated super signature) [Yun Xiaoduo]
TypeScript 发布 4.8 beta 版本
随机推荐
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
Ctfshow, information collection: web8
STM32F103C8T6 PWM驱动舵机(SG90)
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
2. Heap sort "hard to understand sort"
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
[server data recovery] data recovery case of raid failure of a Dell server
Android -- jetpack: the difference between livedata setValue and postvalue
2. 堆排序『较难理解的排序』
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
Ctfshow, information collection: web4
Window环境下配置Mongodb数据库
【搞船日记】【Shapr3D的STL格式转Gcode】
MySQL bit type resolution
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
Configure mongodb database in window environment
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟