当前位置:网站首页>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
边栏推荐
- Yunxiaoduo software internal test distribution test platform description document
- MySQL bit type resolution
- Keil5 does not support online simulation of STM32 F0 series
- Getting started with webgl (3)
- [quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
- 2. Heap sort "hard to understand sort"
- 【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
- 【搞船日记】【Shapr3D的STL格式转Gcode】
- 【原创】一切不谈考核的管理都是扯淡!
- 2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
猜你喜欢
Briefly describe the working principle of kept
Getting started with webgl (2)
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
使用Scrapy框架爬取网页并保存到Mysql的实现
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
The download button and debug button in keil are grayed out
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
Win10 or win11 taskbar, automatically hidden and transparent
什麼是數據泄露
随机推荐
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
15. Using the text editing tool VIM
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
Stm32f103c8t6 PWM drive steering gear (sg90)
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
The rebound problem of using Scrollview in cocos Creator
Ctfshow, information collection: web8
Use cpolar to build a business website (2)
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
大表delete删数据导致数据库异常解决
全日制研究生和非全日制研究生的区别!
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
How to create Apple Developer personal account P8 certificate
Pit avoidance: description of null values in in and not in SQL
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
Comparable and comparator of sorting
【搞船日记】【Shapr3D的STL格式转Gcode】
Win10 or win11 taskbar, automatically hidden and transparent