【问题标题】:Launch command in Tkinter based on selected radio button?基于选定的单选按钮在 Tkinter 中启动命令?
【发布时间】:2012-05-23 14:29:54
【问题描述】:

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

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

【问题讨论】:

    标签: python command radio-button tkinter


    【解决方案1】:

    您正在做的是调用函数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,所以您可能会明白。

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

    【讨论】:

    • 这就像一个魅力。我不完全理解为什么pomocommand 作为def pomocommand 中的变量可以工作。感谢您的建议阅读。 :)
    • 是的,没用。尝试在这个函数中做print pomocommand。你会看到它是一个整数或 None 或类似的东西。这就是 Button 类的函数configure 返回的内容。如果您不想对它做任何事情,您很可能不需要将其存储在变量中。我只是复制粘贴了您写的内容...:P您可以将其删除。
    • 啊,好吧。感谢您的帮助!
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2021-02-13
    • 2022-08-14
    • 2019-04-22
    • 2017-12-28
    相关资源
    最近更新 更多