当前位置:网站首页>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
边栏推荐
- [deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
- Oracle控制文件丢失恢复归档模式方法
- MongoDB数据库基础知识整理
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
- What is Base64?
- OpenGL common functions
- Jacobo code coverage
- Database exception resolution caused by large table delete data deletion
- Bye, Dachang! I'm going to the factory today
猜你喜欢
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
使用cpolar建立一个商业网站(2)
什么是数据泄露
"Baidu Cup" CTF competition 2017 February, web:include
全日制研究生和非全日制研究生的区别!
Ctfshow, information collection: web13
从 1.5 开始搭建一个微服务框架链路追踪 traceId
Guangzhou Development Zone enables geographical indication products to help rural revitalization
Write sequence frame animation with shader
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
随机推荐
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
Pit avoidance: description of null values in in and not in SQL
Wechat applet 01
Getting started with webgl (3)
什么是pv和uv? pv、uv
Nacos一致性协议 CP/AP/JRaft/Distro协议
STM32F103C8T6 PWM驱动舵机(SG90)
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
MongoDB数据库基础知识整理
[make a boat diary] [shapr3d STL format to gcode]
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Ctfshow, information collection: web10
What is data leakage
Getting started with webgl (1)
#HPDC智能基座人才发展峰会随笔
How to create Apple Developer personal account P8 certificate
大表delete删数据导致数据库异常解决