是否基于选定的单选按钮在Tkinter中启动命令?

2024-06-09 05:54:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想更改按钮的功能和文本,根据选择的单选按钮。现在发生的事情是,两个命令在应用程序启动时同时运行,而不是基于选择哪个单选按钮。

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

Tags: totextselfiftimeconfigurebuttonlabel
1条回答
网友
1楼 · 发布于 2024-06-09 05:54:49

您所做的是调用函数self.button.configure(...),而不是传递函数本身。下面是一个小例子:

def test(a):
    return a+4

callback_1 = test(2) # callback_1 will be (2+4) = 6, because you called the function. Notice the parens ()
callback_2 = test    # callback_2 will be the function test, you did not call it
# So, you can do:
callback_2(some_value) # will return (some_value + 4), here you called it

基本上,发生的事情是您正在使用第一个示例,因此函数在__init__中被调用,它应该在那里执行。你想要的是类似于第二个的东西,因为Tkinter想要调用一些东西(一个函数或任何callable)。所以pomocommandbreak应该是函数,而不是调用函数的结果。(您可以尝试执行print pomocommand,并看到它不是一个函数。)

解决方案是创建一个新函数,或者使用lambdas。这里:

def pomocommand(self):
    pomocommand = self.button.configure(text="Pomodoro", state=tk.NORMAL, command= lambda: self.pomodoro(pomo)) #Switch back to the pomodoro timer

# and in your __init__ method:
def __init__(self):
    # ...
    self.radio = tk.Radiobutton(self, text="Pomodoro", variable = self.radvar, value=1, indicatoron=0, command = self.pomocommand)
    # ...

另一个按钮也一样。不建议在此处使用lambda,因为它是一个长语句(self.button.configure部分),因此您的代码将不可读。但我看到你在用兰姆达斯,所以你可能会有这个想法。

顺便说一下,像你一样使用lambda时要小心。这里pomo是一个全局变量,但如果不是,您可能会遇到麻烦。见this question。(现在没关系,所以你可以忽略这个:D)。

相关问题 更多 >