当前位置:网站首页>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
 Insert picture description here
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

原网站

版权声明
本文为[Mh_ Python_ learn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130612130687.html