Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据所选单选按钮在Tkinter中启动命令?_Python_Command_Radio Button_Tkinter - Fatal编程技术网

Python 根据所选单选按钮在Tkinter中启动命令?

Python 根据所选单选按钮在Tkinter中启动命令?,python,command,radio-button,tkinter,Python,Command,Radio Button,Tkinter,我想根据选择的单选按钮更改按钮的功能和文本。现在的情况是,应用程序一启动,两个命令就同时运行,而不是根据选择的单选按钮来运行 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__(s

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

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()
将tkinter作为tk导入
导入时间
##波莫多罗河的时间变量
pomo=60*25##60秒乘以分钟数
b时间=60*5##60
类示例应用程序(tk.tk):
定义初始化(自):
tk.tk.\uuuuu初始化(self)
self.label=tk.label(self,text=“25:00”,width=10,font=“Helvetica 20”)
self.label.pack()
自剩余=0
self.button=tk.button(self)
self.button.pack()
pomocommand=self.button.configure(text=“Pomodoro”,state=tk.NORMAL,command=lambda:self.Pomodoro(pomo))#切换回Pomodoro定时器
breakcommand=self.button.configure(text=“Break”,state=tk.NORMAL,command=lambda:self.breaktime(btime))#切换到中断计时器
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=“pomotoro”,变量=self.radvar,值=1,指示符=0,命令=pomocommand)
self.radio.pack(anchor=tk.W)
self.radio=tk.Radiobutton(self,text=“Counter”,variable=self.radvar,value=2,indicatoron=0,command=countercommand)
自无线电组(侧=左侧)
def pomodoro(自身,剩余=无):
self.button.configure(状态=tk.DISABLED)
如果剩余值不是无:
自我剩余=剩余

如果self.remaining您所做的是调用函数
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
基本上,您使用的是第一个示例,因此该函数在
\uuuu init\uuuu
中调用,它应该在那里执行。您想要的与第二个类似,因为Tkinter想要调用某个东西(一个函数或任何可调用的
)。因此,
pomocommand
break
应该是函数,而不是调用函数的结果。(您可以尝试执行
打印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部分),因此代码不可读。但是我看到你正在使用lambdas,所以你可能会有这个想法


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

这就像一个符咒。我不完全理解为什么
pomocommand
作为
def pomocommand
中的一个变量起作用。谢谢你的建议阅读。:)是的,没用。尝试在此函数内执行
打印pomocommand
。你会看到它是一个整数或无或类似的东西。这是Button类的函数
configure
返回的结果。如果您不想对它做任何事情,您很可能不需要将它存储在变量中。我只是复制粘贴了你写的东西…:你可以把它取下来。啊,好的。谢谢你的帮助!