Launch command in Tkinter based on selected radio button? Launch command in Tkinter based on selected radio button? tkinter tkinter

Launch command in Tkinter based on selected radio button?


What you are doing is calling the function self.button.configure(...) instead of passing the function itself. Here is a small example:

def test(a):    return a+4callback_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

Basically what is happening is that you are using the first example, so the function is called in __init__, and what it is supposed to do is done there. What you want is something similar to the second, because Tkinter wants something to call (a function or any callable). So pomocommand and break should be functions not the result of calling a function. (You can try to do print pomocommand and see that it is not a function.)

The solution is either to create a new function, or use lambdas. Here:

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)    # ...

And you do the same for the other button. Using a lambda here is not recommended because it is a long statement (the self.button.configure part) so your code will not be readable. But I see you are using lambdas, so you might get the idea.

As a side note, be careful when using lambda like you do. Here pomo is a global variable, but if it is not, you might get into trouble. See this question. (Right now it is alright, so you can just ignore this :D).