我想更改按钮的功能和文本,根据选择的单选按钮。现在发生的事情是,两个命令在应用程序启动时同时运行,而不是基于选择哪个单选按钮。
import tkinter as tk
import time
## Time variables for the Pomodoro
pomo = 60 * 25 ## 60 seconds times number of minutes
btime = 60 * 5 ## 60
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="25:00", width=10, font="Helvetica 20")
self.label.pack()
self.remaining = 0
self.button = tk.Button(self)
self.button.pack()
pomocommand = self.button.configure(text="Pomodoro", state=tk.NORMAL, command= lambda: self.pomodoro(pomo)) #Switch back to the pomodoro timer
breakcommand = self.button.configure(text="Break", state=tk.NORMAL, command= lambda: self.breaktime(btime)) #Switch to the break timer
countercommand = self.button.configure(text="Counter", state=tk.NORMAL, command= print('cheese'))
self.radvar = tk.IntVar()
self.radvar.set('1')
self.radio = tk.Radiobutton(self, text="Pomodoro", variable = self.radvar, value=1, indicatoron=0, command = pomocommand)
self.radio.pack(anchor=tk.W)
self.radio = tk.Radiobutton(self, text="Counter", variable = self.radvar, value=2, indicatoron=0, command = countercommand)
self.radio.pack(side=tk.LEFT)
def pomodoro(self, remaining = None):
self.button.configure(state=tk.DISABLED)
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="Time's up!")
breakcommand
else:
self.label.configure(text= time.strftime('%M:%S', time.gmtime(self.remaining))) #Integer to 'Minutes' and 'Seconds'
self.remaining = self.remaining - 1
self.after(1000, self.pomodoro)
def breaktime(self, remaining = None):
self.button.configure(state=tk.DISABLED)
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="Time's up!")
pomocommand
else:
self.label.configure(text= time.strftime('%M:%S', time.gmtime(self.remaining))) #Integer to 'Minutes' and 'Seconds'
self.remaining = self.remaining - 1
self.after(1000, self.breaktime)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
您所做的是调用函数
self.button.configure(...)
,而不是传递函数本身。下面是一个小例子:基本上,发生的事情是您正在使用第一个示例,因此函数在
__init__
中被调用,它应该在那里执行。你想要的是类似于第二个的东西,因为Tkinter想要调用一些东西(一个函数或任何callable
)。所以pomocommand
和break
应该是函数,而不是调用函数的结果。(您可以尝试执行print pomocommand
,并看到它不是一个函数。)解决方案是创建一个新函数,或者使用lambdas。这里:
另一个按钮也一样。不建议在此处使用lambda,因为它是一个长语句(self.button.configure部分),因此您的代码将不可读。但我看到你在用兰姆达斯,所以你可能会有这个想法。
顺便说一下,像你一样使用lambda时要小心。这里
pomo
是一个全局变量,但如果不是,您可能会遇到麻烦。见this question。(现在没关系,所以你可以忽略这个:D)。相关问题 更多 >
编程相关推荐