单击其他按钮时,为什么单击按钮的事件处理程序会停止?

时间:2014-07-01 19:04:13

标签: python python-2.7

在这个程序中,我希望有时间继续按下每个按钮,即使点击了另一个按钮,但是,当另一个按钮时,时间似乎停在按钮上我有任何建议吗?

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()

1 个答案:

答案 0 :(得分:1)

你的问题是来自tkinter的 按钮 只是在用户点击该按钮时才实现命令功能。因此,当用户点击另一个按钮时,该命令功能将停止/终止并切换到单击的按钮命令功能。这就是你的计时器停止的主要原因!

此问题有两种解决方案:

  1. 最简单的方法是将计时器(例如Button1分钟,秒,Button2分钟,秒等)存储为属性,并使用tkinter Label 显示计时器它们中的每一个都在界面上,而不是解决有关 按钮 的问题,因为只要用户点击按钮,命令功能将始终停止并更改为另一个。

  2. 解决此问题的另一种方法是使用 按钮 小部件中的.invoke()方法从之前单击的按钮回调命令功能。如果您想使用这种方式,可以在此链接上查看此方法的工作原理:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/button.html

  3. P / S:我也喜欢英雄联盟!

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

    这是我的计时器代码演示,所以请查看!我使用.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()