Python 为什么我的按钮单击事件处理程序在单击其他按钮时停止?
在这个程序中,我希望在每个按钮上都有时间继续,即使单击了另一个按钮,但当另一个按钮被单击时,时间似乎停止了。我有什么建议吗Python 为什么我的按钮单击事件处理程序在单击其他按钮时停止?,python,python-2.7,Python,Python 2.7,在这个程序中,我希望在每个按钮上都有时间继续,即使单击了另一个按钮,但当另一个按钮被单击时,时间似乎停止了。我有什么建议吗 from Tkinter import * import os import time class Application(Frame): ########################################################################## def my_timeDrag(self): # creates a
from Tkinter import *
import os
import time
class Application(Frame):
##########################################################################
def my_timeDrag(self): # creates a timer starting at 5 min , counts down to 0 then repeats
min = 5
sec = 59
while sec <=60:
self.Button3.configure(text="{}:{}".format(min,sec))
self.Button3.update()
os.system('cls')
print min, "Minutes", sec, "Seconds"
time.sleep(1)
sec -= 1
if sec == 0:
min -= 1
sec = 59
elif min == 0:
min = 5
##########################################################################################
def my_timeBB(self): # creates a timer starting at 5 min , counts down to 0 then repeats
min = 4
sec = 59
while sec <=60:
self.Button1.configure(text="{}:{}".format(min,sec))
self.Button1.update()
os.system('cls')
print min, "Minutes", sec, "Seconds"
time.sleep(1)
sec -= 1
if sec == 0:
min -= 1
sec = 59
elif min == 0:
min = 4
#######################################################
def my_timeRB(self): # creates a timer starting at 5 min , counts down to 0 then repeats
min = 4
sec = 59
while sec <=60:
self.Button2.configure(text="{}:{}".format(min,sec))
self.Button2.update()
os.system('cls')
print min, "Minutes", sec, "Seconds"
time.sleep(1)
sec -= 1
if sec == 0:
min -= 1
sec = 59
elif min == 0:
min = 4
########################################################
def createButtons(self): # creats a button
self.Button1 = Button(self)
self.Button1["text"] = "Blue Buff"
self.Button1["fg"] = "Blue"
self.Button1["command"] = self.my_timeBB # suppose to implement countdown in button text when click.
self.Button1.pack({"side": "left"})
self.Button2 = Button(self)
self.Button2["text"] = "Red Buff"
self.Button2["fg"] = "Red"
self.Button2["command"] = self.my_timeRB
self.Button2.pack({"side":"right"})
self.Button2.pack(padx=50)
self.Button3 = Button(self)
self.Button3["text"] = " Dragon "
self.Button3["fg"] = "Pink"
self.Button3["bg"] = "Purple"
self.Button3["command"] = self.my_timeDrag
self.Button3.pack(side="bottom",pady=50)
self.Quit = Button(self)
self.Quit["text"] = "Quit"
self.Quit["command"] = self.quit
self.Quit.pack()
##############################################################
##############################################################
def __init__(self, master=None):
Frame.__init__(self, master) # initializes window
self.pack()
self.createButtons()
root = Tk()
root.title("Jungle Timer by BabyAchilles")
root.geometry("400x300")
app = Application(master=root)
root.mainloop()
从Tkinter导入*
导入操作系统
导入时间
课程申请(框架):
##########################################################################
def my_timeDrag(self):#创建一个从5分钟开始的计时器,倒计时到0,然后重复
最小值=5
秒=59
而sec您的问题是,tkinter的按钮仅在用户单击该按钮时执行命令功能。因此,当用户单击另一个按钮时,该命令功能将停止/终止并更改为单击的按钮命令功能。这就是你的计时器停止的主要原因
此问题有两种解决方案:
最简单的方法是存储计时器(例如按钮1分钟、秒、按钮2分钟、秒等)作为属性,并使用tkinter标签在界面上显示每个属性的计时器,而不是处理有关按钮的问题,因为每当用户单击按钮时,命令功能将始终停止并更改为另一个
解决此问题的另一种方法是使用按钮小部件中的.invoke()
方法从以前单击的按钮调用命令函数。如果您想使用这种方法,您可以查看此链接了解此方法的工作原理:
我也喜欢传奇联盟
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是我的计时器代码演示,请查看!我对根窗口使用了.after()
方法。此外,我将所有时间数据存储为对象属性,因此更易于访问
import tkinter
import time
DEFAULT_FONT = ('Helvetica',30)
class LoL_JungleTimer():
def __init__(self):
self._root_window = tkinter.Tk()
Dragon_button = tkinter.Button(master = self._root_window, text = 'Dragon', fg = 'purple', command = self._dragon_start)
BlueBuff_button = tkinter.Button(master = self._root_window, text = 'Blue Buff', fg = 'blue', command = self._blue_buff_start)
RedBuff_button = tkinter.Button(master = self._root_window, text = 'Red Buff', fg = 'red', command = self._red_buff_start)
self._blue_buff_label = tkinter.Label(master = self._root_window, text = '5:00', fg = 'blue', font = DEFAULT_FONT)
self._red_buff_label = tkinter.Label(master = self._root_window, text = '5:00', fg = 'red', font = DEFAULT_FONT)
self._dragon_label = tkinter.Label(master = self._root_window, text = '6:00', fg = 'purple', font = DEFAULT_FONT)
Dragon_button.grid(row = 0, column = 0, padx = 10, pady = 10)
BlueBuff_button.grid(row = 1, column = 0, padx = 10, pady = 10)
RedBuff_button.grid(row = 2, column = 0, padx = 10, pady = 10)
self._blue_buff_label.grid(row = 1, column = 1, padx = 10, pady = 10)
self._red_buff_label.grid(row = 2, column = 1, padx = 10, pady = 10)
self._dragon_label.grid(row = 0, column = 1, padx = 10, pady = 10)
self.drag_minute = 5
self.drag_second = 59
self.BB_minute = 4
self.BB_second = 59
self.RB_minute = 4
self.RB_second = 59
def run(self):
self._root_window.mainloop()
def _time_counter(self, minutes, seconds):
if seconds < 60:
seconds -= 1
if seconds == 0:
seconds = 59
minutes -= 1
return minutes, seconds
def _blue_buff_start(self):
self._blue_buff_label.configure(text = "{0}:{1:02d}".format(self.BB_minute,self.BB_second))
self._root_window.update()
self.BB_minute,self.BB_second = self._time_counter(self.BB_minute,self.BB_second)
self._root_window.after(1000, func = self._blue_buff_start)
def _dragon_start(self):
self._dragon_label.configure(text = "{0}:{1:02d}".format(self.drag_minute,self.drag_second))
self._root_window.update()
self.drag_minute,self.drag_second = self._time_counter(self.drag_minute,self.drag_second)
self._root_window.after(1000, func = self._dragon_start)
def _red_buff_start(self):
self._red_buff_label.configure(text = "{0}:{1:02d}".format(self.RB_minute,self.RB_second))
self._root_window.update()
self.RB_minute,self.RB_second = self._time_counter(self.RB_minute,self.RB_second)
self._root_window.after(1000, func = self._red_buff_start)
if __name__ == '__main__':
LoL_JungleTimer().run()
导入tkinter
导入时间
默认字体=('Helvetica',30)
类LoL_JungleTimer():
定义初始化(自):
self.\u root\u window=tkinter.Tk()
Dragon\u button=tkinter.button(master=self.\u root\u窗口,文本='Dragon',前景='purple',命令=self.\u Dragon\u start)
BlueBuff_button=tkinter.button(master=self.\u root\u窗口,文本='Blue Buff',前景='Blue',命令=self.\u Blue\u Buff\u start)
RedBuff\u button=tkinter.button(master=self.\u root\u窗口,text='Red Buff',fg='Red',command=self.\u Red\u Buff\u start)
self.\u blue\u buff\u label=tkinter.label(master=self.\u root\u窗口,文本='5:00',前景='blue',字体=默认字体)
self.\u red\u buff\u label=tkinter.label(master=self.\u root\u窗口,文本='5:00',前景='red',字体=默认字体)
self.\u dragon\u label=tkinter.label(master=self.\u root\u窗口,文本='6:00',前景='purple',字体=默认字体)
Dragon_按钮网格(行=0,列=0,padx=10,pady=10)
BlueBuff_按钮网格(行=1,列=0,padx=10,pady=10)
网格(行=2,列=0,padx=10,pady=10)
self.\u blue\u buff\u label.grid(行=1,列=1,padx=10,pady=10)
self.\u red\u buff\u label.grid(行=2,列=1,padx=10,pady=10)
self.\u dragon\u label.grid(行=0,列=1,padx=10,pady=10)
自身阻力_分钟=5
self.drag_second=59
self.BB_分钟=4
self.BB_秒=59
self.RB_分钟=4
self.RB_秒=59
def运行(自):
self.\u root\u window.mainloop()
定义时间计数器(自身、分、秒):
如果秒数小于60:
秒-=1
如果秒=0:
秒=59
分钟-=1
返回分钟,秒
def_蓝色_buff_启动(自):
self._blue_buff_label.configure(text=“{0}:{1:02d}.”格式(self.BB_分钟,self.BB_秒))
self.\u root\u window.update()
self.BB_分钟,self.BB_秒=self.\u时间计数器(self.BB_分钟,self.BB_秒)
self.\u root\u window.after(1000,func=self.\u blue\u buff\u start)
def_dragon_启动(自):
self.\u dragon\u label.configure(text=“{0}:{1:02d}”。格式(self.drag\u minute,self.drag\u second))
self.\u root\u window.update()
self.drag\u分钟,self.drag\u秒=self.\u时间\u计数器(self.drag\u分钟,self.drag\u秒)
self.\u root\u window.after(1000,func=self.\u dragon\u start)
def_红色_buff_启动(自):
self._red_buff_label.configure(text=“{0}:{1:02d}.”格式(self.RB_分钟,self.RB_秒))
self.\u root\u window.update()
self.RB_分钟,self.RB_秒=self.\u时间计数器(self.RB_分钟,self.RB_秒)
self.\u root\u window.after(1000,func=self.\u red\u buff\u start)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
LoL_JungleTimer().run()
非常感谢!谢谢你的帮助,哈哈,如果你想给我加上我的SN是BabyÄres。再次谢谢。如果你能给我更多的提示,我会很感激没有太多关于invoke方法的信息,而且标签不支持命令方法,所以我又卡住了。我只是用我为丛林定时器创建的一个小演示更新了我的答案。它没有完成;但是,它解决了您关于按钮的问题。如果你不理解我的演示,请随时问我!非常感谢,我仍在查看您在这里所拥有的内容,以尝试完全了解正在发生的事情。如果可以的话,我明天会回复你我可能有的任何问题,再次感谢你!所以我对我的程序做了一些重新安排,现在它更整洁了,但我仍然不能让所有的时间都在一起工作。不过,我确实让它们出现在标签中,要激活它,请单击一个按钮。我注意到在你的节目里你也做了同样的事情?那么,如果每次点击按钮都会停止上一个按钮,那么它怎么能与按钮一起工作呢?再次感谢您的持续帮助。