【问题标题】:How to stop this after function in python tkinter如何在 python tkinter 中的函数后停止此操作
【发布时间】:2020-01-09 00:08:06
【问题描述】:

我正在尝试制作一个秒表程序,每当我按下按钮运行它时,函数 def watch() 会一直自行执行,我无法在需要时停止它。

有什么方法可以在按下按钮后停止执行 def watch() 函数?

谢谢你...

from tkinter import *

root = Tk()

tog = 0

hour = 0
mins = 0
sec = 0


def toggle():
    global tog
    tog = tog + 1

    if tog == 1:
        watch()
    elif tog == 2:
        stop()
        tog = 0

def stop():
    donothing = 0

def watch():
    global sec
    global hour
    global mins

    sec = sec + 1
    l1.config(text=sec)
    l1.after(1000,watch)



    l1 = Label(root)
    l1.pack()

Button(root,text="Start",command= lambda: toggle()).pack()

root.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您应该保留对after 调用的引用,并在toggleFalse 时取消回调。您可以通过使用 tkinter 变量来避免丑陋的 global 声明,这些变量是可以读取或设置值的对象。

    import tkinter as tk
    
    
    def toggle_on_off():
        toggle.set(not toggle.get())
        if toggle.get():
            watch()
    
    def watch():
        count_seconds.set(count_seconds.get() + 1)
        if toggle.get():
            _callback_id.set(root.after(1000, watch))
        else:
            root.after_cancel(_callback_id.get())
            
    root = tk.Tk()
    
    count_seconds = tk.IntVar(root)
    count_seconds.set(0)
    toggle = tk.BooleanVar(root)
    toggle.set(False)
    
    Button(root,text="Start",command=toggle_on_off).pack()
    label = tk.Label(root, textvariable=count_seconds)
    label.pack()
    
    _callback_id = tk.StringVar(root)
    _callback_id.set(None)
    
    root.mainloop()
    

    [编辑]

    与全局相同的代码是这样的:

    import tkinter as tk
    
    
    def toggle_on_off():
        global toggle
        toggle = not toggle
        if toggle:
            watch()
    
    def watch():
        global count_seconds, _callback_id
        count_seconds += 1
        label.configure(text=str(count_seconds))
        if toggle:
            _callback_id = root.after(1000, watch)
        else:
            root.after_cancel(_callback_id)
            
    root = tk.Tk()
    
    count_seconds = 0
    toggle = False
    
    Button(root,text="Start",command=toggle_on_off).pack()
    label = tk.Label(root, text=str(count_seconds))
    label.pack()
    
    _callback_id = None
    
    root.mainloop()
    

    【讨论】:

    • 这段代码看起来有点混乱,实际上我是一个新的 python 编码器,所以使用全局变量对我来说非常容易..顺便感谢您的时间
    • 好的@Bittu,我明白了。我添加了一个带有全局变量的版本。保持两者,盯着代码看,直到你理解它会大大提高你的编程能力。
    【解决方案2】:

    您可以添加一个全局变量,例如“退出”,

    并在 l1.after(1000,watch) 之前添加 if 语句

    def toggle():
        global tog, exit
        exit = False
        tog = tog + 1
    
        if tog == 1:
            watch()
        elif tog == 2:
            stop()
            tog = 0
    
    
    def watch():
        global sec
        global hour
        global mins
        global exit
    
        sec = sec + 1
        l1.config(text=sec)
        if not exit:
            l1.after(1000,watch)
    

    在stop()中,可以写

    def stop():
        global exit
        exit = True
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2020-02-16
      • 1970-01-01
      • 2020-05-28
      • 2022-01-21
      • 2020-06-06
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      相关资源
      最近更新 更多