首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >暂停按钮不工作,我如何正确地使用after_cancel?

暂停按钮不工作,我如何正确地使用after_cancel?
EN

Stack Overflow用户
提问于 2021-07-08 09:54:32
回答 2查看 85关注 0票数 0

所以我正在制作一个有计时器和计时器工作的程序,现在我使用的是暂停功能。经过一些研究,我发现了一个叫做after_cancel的函数。这个函数应该取消后函数,因为在这种情况下,after函数会创建一个无限循环。在这种情况下,我如何正确地使用after_cancel,或者有没有其他可能的解决方案?

提前谢谢。

代码语言:javascript
运行
复制
t = 60000

global timerState
timerState = True

def pause():
    timerLabel.after_cancel(countdown)
    timerState = False
    timerButton.config(text="Play", command=countdown)


def countdown():
    global t

    if t == 0:
        timer = "00:00"
        timerLabel.config(text=timer)
        return

    if timerState == False:
        timerLabel.after_cancel(countdown)
        timerButton.config(text="Play", command=countdown)
        return

    mins = t / 60000

    secs = t / 1000
    secs = secs - int(mins) * 60

    mills = t

    mills = mills - int(secs) * 1000



    if timerState == True:
        timer = "{:02d}:{:02d}".format(int(mins),int(secs))
        timerLabel.config(text=timer)
        t -= 1
        timerLabel.after(1, countdown)

        timerButton.config(text="Pause", command=pause)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-08 10:15:43

大多数情况下,只使用.after_cancel语句就可以避免使用if脚本。例如,请看以下内容:

代码语言:javascript
运行
复制
import tkinter as tk

t = 60000

def pause():
    global timerState
    timerState = False
    timerButton.config(text="Play", command=start_countdown)

def start_countdown():
    global timerState
    timerState = True
    timerButton.config(text="Pause", command=pause)
    countdown()

def countdown():
    global t

    if timerState:
        timerLabel.config(text=t)
        t -= 1
        if t > 0:
            timerLabel.after(1, countdown)


root = tk.Tk()

timerLabel = tk.Label(root, text="")
timerLabel.pack()

timerButton = tk.Button(root, text="Play", command=start_countdown)
timerButton.pack()

root.mainloop()

我修改了您的代码以显示t,而不使用mm:ss格式。要点是,如果timerStateFalse,则永远不会调用timerLabel.after(1, countdown),因此没有必要使用.after_cancel

注意:您还没有考虑到其他代码所需的时间,所以t实际上不会以毫秒为单位(至少对于我的慢计算机而言)。

票数 1
EN

Stack Overflow用户

发布于 2021-07-08 11:17:45

下面是afterafter_cancel的演示

为了清除事件队列,需要取消每个after

在这个程序中,每次按下按钮,都会产生时间延迟事件。事件ID存储在self.after_time中。

我已经设置延迟值增加100毫秒与每个按钮按下,为演示目的。它把主人从视野中撤回。

当时间延迟事件完成时,它调用self.action

self.actionafter_cancel( self.after_time )取消事件,并使母版可见,准备下一个按钮按下。

代码语言:javascript
运行
复制
import tkinter

class after_demo:

    delay = 100

    def __init__( self ):

        self.master = tkinter.Tk()
        self.master.title( 'After Demo' )
        self.control = tkinter.Button(
            self.master, text = 'Begin Demo',
            width = 40, command = self.pause )
        self.control.grid(row=0,column=0,sticky='nsew')

    def action( self ):

        self.master.after_cancel( self.after_time )
        self.control[ 'text' ] = 'Delay( {} ) ms'.format( self.delay )

        self.master.deiconify()
        
    def pause( self ):

        self.after_time = self.master.after( self.delay, self.action )
        self.delay += 100

        self.master.withdraw()

if __name__ == '__main__':

    timer = after_demo( )
    tkinter.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68299362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档