如何解决如何在Python中覆盖计时器
我有2个按钮(旋转和反向)。当其中一个按钮打开时,另一个按钮将被禁用,因为两个按钮不能同时打开。当“打开”按钮关闭时,它会在禁用按钮10秒钟后返回“正常”。
我的问题是,如果我要在10秒计时器之前按下ON(关闭相同的按钮),如何停止10秒计时器。例如,如果我打开旋转然后关闭(这将启动10秒反向计时器),但是如果决定再次打开旋转,我的反向按钮计时器仍在计数。我该如何停止计时器,以便再次关闭计时器就可以重新启动10秒?
\\
from tkinter import *
from tkinter import font
from threading import Timer
from time import sleep
reverse_motor = True
rotate_motor = True
def hp30():
global rotate_motor
# Turn ON 30hp Motor
if rotate_motor:
rotate_motor = False
motor30hp.config(activebackground=motor30hp.cget('background'))
reverseButton["state"] = DISABLED
motor30hp["text"] = "ON"
motor30hp["bg"] = "green"
print("30hp Motor ON")
motor30hp.config(activebackground=motor30hp.cget('background'))
else:
# Turn OFF 30hp Motor
rotate_motor = True
motor30hp["text"] = "OFF"
motor30hp["bg"] = "red"
print("30hp Motor OFF")
motor30hp.config(activebackground=motor30hp.cget('background'))
tim01 = Timer(10,normal_reverse)
tim01.start()
print("Reverse 10 sec Timer On")
def reverse():
global reverse_motor
if reverse_motor:
reverse_motor = False
motor30hp["state"] = DISABLED
print("Rotate Disabled")
reverseButton.config(activebackground=reverseButton.cget('background'))
print("Reverse Motor ON")
reverseButton["text"] = "ON"
reverseButton["bg"] = "green"
else:
reverse_motor = True
reverseButton["text"] = "OFF"
reverseButton["bg"] = "red"
reverseButton.config(activebackground=reverseButton.cget('background'))
print("Reverse Motor OFF")
timer = Timer(10,normal_hp30)
timer.start()
print("Rotate 10 sec Timer On")
def normal_hp30():
motor30hp.config(state='normal')
print("Rotate Enabled")
def normal_reverse():
reverseButton.config(state='normal')
print("Reverse Enabled")
root = Tk()
root.title("SOTS Remote")
root.geometry('400x150+0+0')
root.configure(background="black")
myFont = font.Font(family='Times',size=30,weight='bold')
#-------------------------------lABELS--------------------------------------------------------
root.label2 = Label(root,text="Rotate",font=myFont,fg="white",bg="black")
root.label2.grid(row=0,column=1,sticky="W",padx=25,pady=2)
root.label3 = Label(root,text="Reverse",bg="black",padx=30)
root.label3.grid(row=0,column=0,sticky="NW",pady=2)
#----------------------------------------buttons--------------------------------------------------
reverseButton = Button(root,text="OFF",height=1,width=7,bg='dodgerblue',command=reverse,relief=RIDGE,bd=5)
reverseButton.grid(row=1,sticky="NW")
reverseButton.config(activebackground=reverseButton.cget('background'))
motor30hp = Button(root,bg='red',bd=5,command=hp30,width=7)
motor30hp.grid(row=1,sticky="W")
motor30hp.config(activebackground=motor30hp.cget('background'))
root.mainloop()
\\
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。