This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 1 year ago.
I want to run function when I click on tabs.
For example,
I have two tabs
def refresh():
if str(notebook.index(notebook.select())) == "2":
refresh2()
else:
refresh1()
notebook.bind("<<NotebookTabChanged>>", refresh())
Doesn't work.
How can I fix it ?
You always need to provide the reference of the function, not call the function. Doing refresh() will call the function. And when the function is executed, the returned value will be assigned. In your case, it is None or nothing.
So, you are basically telling tkinter to do nothing when the "<<NotebookTabChanged>>" event is raised.
def refresh():
if str(notebook.index(notebook.select())) == "2":
refresh2()
else:
refresh1()
notebook.bind("<<NotebookTabChanged>>", refresh)
Related
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 9 months ago.
I have three dropdown menus and 3 string variables that will take on the value of the menus, However in the function that I define query, it seems as if the string variables do not take on the value each time I select a new one on the dropdown menu. It will print the set value and do nothing else. Also what's weird is that it was working perfectly and then suddenly didn't work at all.
Really need help on this one thanks.
root=Tk()
root.title('Query')
root.geometry('600x400')
def query():
print(clicked_id_mouse.get())
print(clicked_id_filename.get())
print(clicked_id_researcher.get())
clicked_id_mouse=StringVar()
clicked_id_mouse.set("Choose Mouse Id")
drop_mouse= OptionMenu(root,clicked_id_mouse,*mouse_id)
drop_mouse.place(x=50,y=150)
clicked_id_researcher=StringVar()
clicked_id_researcher.set("Choose Researcher")
researcher= OptionMenu(root,clicked_id_researcher,*r_i)
researcher.place(x=200,y=150)
clicked_id_filename=StringVar()
clicked_id_filename.set("Choose Filename")
filename= OptionMenu(root,clicked_id_filename,*filename)
filename.place(x=350,y=150)
button= Button(root,text="Query",command=query())
button.place(x=210,y=200)
root.mainloop()
When you are assigning a command to a button use "lambda: ...". Otherwise it will execute it only once the button is created. Your code should work as intended by changing
button= Button(root,text="Query",command=query())
into
button= Button(root,text="Query",command=lambda: query())
I hope that helps.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
When I try to add a command to a button it runs the command without me pressing the button.
How do I fix this?
For example, if I did this:
import tkinter
root = tkinter.Tk()
def a():
print("Hello")
button = tkinter.Button(root,command=a())
button.pack()
root.mainloop()
and ran it, it would execute the function a() without me pressing the button.
tkinter.Button(root,command=a())
You should pass the function, not call it, so remove the ():
tkinter.Button(root,command=a)
This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
How to pass arguments to a Button command in Tkinter?
(18 answers)
Closed 2 years ago.
I was creating a sort of button-based paint with tkinter, so I wanted to give every button a command to paint itself.
for i in range(xc*yc):
grid.append(Button(wn,text=" ",bg="white",padx=5,pady=5))
grid[-1].config(command = paint(i)) <--????
grid[-1].place(x= (i%xc) * 30 +60, y = (math.floor(i/xc) * 30)+30)
The problem is that every button recieves the command "paint(i)" with the final value of i, so everytime it paints the last button
Use root.update() method forecefully updates UI.so you can use that to see every iteration of for loop , if you call it inside that
Maybe something like
for i in range(xc*yc):
button = Button(wn,text=" ",bg="white",padx=5,pady=5)
grid.append(button)
button.config(command = paint(i, button)) # note the additional reference to button here!
button.place(x= (i%xc) * 30 +60, y = (math.floor(i/xc) * 30)+30)
would be helpful.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 7 years ago.
I want to delay a function call. (Or in my mind: python is executing the function in the wrong order). In the below example i could write instead of bf(arg) the two functions bf1 and bf2 and it does work as expected: The function is called whenever the button is pressed. But if i include the arguments in the function the function call is executed only once. Returning the function itself doesn't change the behaviour.
Can you please take a look at it and give me a hint where my logic or understanding of python is wrong.
from tkinter import *
def bf(str):
print(str)
# return print(str) #alternative(same behaviour)
main=Tk(screenName='screen',baseName='base')
button1=Button(master=main,text='b1',command=bf("b1"))
button2=Button(master=main,text='b2',command=bf("b2")) # runs once (and runs here)
button1.pack()
button2.pack()
mainloop()
print('end')
--google and stackoverflow search only return things like delay function call for 1 a specific time interval This is not what i am searching for. :-(
The issue is that you are calling the function when you create the buttons, instead of passing a callable that TK will call when the button is clicked. Normally you would just pass the function itself:
button1=Button(master=main, text='b1', command=bf)
but since you want to pass arguments to bf you will need to wrap it in a lambda:
button1=Button(master=main, text='b1', command=lambda: bf('b1'))
What you do in that line is that you don't pass the function but execute it:
button1=Button(master=main,text='b1',command=bf("b1"))
You could either include only the function name, but then you can't pass a parameter to the function:
button1=Button(master=main,text='b1',command=bf)
or you could make use of lambda:
button1=Button(master=main,text='b1',command=lambda:bf("B1"))
This question already has answers here:
All tkinter functions run when program starts
(2 answers)
Closed 7 years ago.
I'm very new to python's tkinter gui and I am trying to use it to build a basic test.
I created the menu where one of the menu items must call a function although when I run the program I can see the output from the function before the menu item has been clicked and when the menu item is clicked it does not call the function.
My code is as follows
from tkinter import *
class cl_main():
def __init__(self, master):
lo_mainmenu = Menu(master)
lo_mainmenu.option_add('*tearOff', FALSE)
master.config(menu=lo_mainmenu)
lo_menugroup = Menu(lo_mainmenu)
lo_mainmenu.add_cascade(label="MenuGroup")
lo_menugroup.add_command(label="Command", command=f_message())
def f_message():
print ("This Function Has Been Called")
root = Tk()
co_main = cl_main(root)
root.mainloop()
I can't see what is wrong with it but I'm sure there is something horribly wrong here
lo_menugroup.add_command(label="Command", command=f_message())
callbacks shouldn't have parentheses. As it is, f_message is called right away and its return value is assigned to command, rather than the function object itself.
lo_menugroup.add_command(label="Command", command=f_message)