如何使用 python 3.8 在 tkinter 中取消关闭延迟计时器的 after 方法
how to cancel the after method for an off delay timer in tkinter using python 3.8
我有一个关闭延迟程序,当我 select 输入复选按钮时,输出为 1。当我取消 select 输入复选按钮时,定时器后输出返回 0 (按比例设置)。为此,我使用 after 方法。那部分有效。我的问题是,如果在输出变为 0 之前再次 select 选中按钮,我想重置计时器;但是一旦检查按钮第一次被 selected,after 方法就会被触发并且不会停止。我正在尝试使用 after_cancel,但无法正常工作。有什么解决办法吗?
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()
预期:在输出变为 0 之前按下复选按钮时,重置计数器。
所以你可以使用 .after_cencel
当 checkbutton 的值为 1
:
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
try: # when the first time you start the counter, root.counter didn't exist, use a try..except to catch it.
root.after_cancel(root.counter)
except :
pass
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
root.counter = out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()
我有一个关闭延迟程序,当我 select 输入复选按钮时,输出为 1。当我取消 select 输入复选按钮时,定时器后输出返回 0 (按比例设置)。为此,我使用 after 方法。那部分有效。我的问题是,如果在输出变为 0 之前再次 select 选中按钮,我想重置计时器;但是一旦检查按钮第一次被 selected,after 方法就会被触发并且不会停止。我正在尝试使用 after_cancel,但无法正常工作。有什么解决办法吗?
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()
预期:在输出变为 0 之前按下复选按钮时,重置计数器。
所以你可以使用 .after_cencel
当 checkbutton 的值为 1
:
from tkinter import *
root = Tk()
t1= IntVar()
out = Label(root, text="0")
remain_time = IntVar()
grab_time = 1000
def start_timer(set_time):
global grab_time
grab_time = int(set_time) * 1000
def play():
if t1.get() == 1:
button1.configure(bg='red')
out.configure(bg="red", text="1")
try: # when the first time you start the counter, root.counter didn't exist, use a try..except to catch it.
root.after_cancel(root.counter)
except :
pass
else:
button1.configure(bg='green')
def result():
out.configure(bg="green", text="0")
root.counter = out.after(grab_time,result)
button1 = Checkbutton(root,variable=t1, textvariable=t1, command=play)
time = Scale(root, from_=1, to=10, command=start_timer)
button1.pack()
time.pack()
out.pack()
root.mainloop()