Skip to main content
 首页 » 编程设计

python之为什么单击不同按钮时我的按钮单击事件处理程序会停止

2025年05月04日14mfrbuaa

在这个程序中,我希望时间在每个按钮上继续,即使另一个按钮被点击,但是,当另一个按钮被点击时,时间似乎停止在一个按钮上,我在这里有什么建议吗?

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 的 Button 只是在用户点击那个按钮时才执行命令功能。因此,当用户单击另一个按钮时,该命令函数将停止/终止并更改为单击的按钮命令函数。这就是计时器停止的主要原因!

这个问题有两种解决方案:

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

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

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