Module's function auto running? [duplicate] - python

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 6 years ago.
I can't figure out why the function that I've called auto runs when I run the script, without pressing the button.
import tkinter
from tkinter import filedialog
root = tkinter.Tk ()
root.title("fool")
root.geometry("300x300")
br = tkinter.Button(root, text ="Carica File", command = filedialog.askopenfile(mode="r"))
br.pack()

Right now, you're passing the result of the call
filedialog.askopenfile(mode="r")
to the command parameter. To be able to get this result, the function is executed and you're seeing the dialog right away. What you probably want to do is just provide the name of a function to call when the button is pressed, so you could define one as
def foo():
filedialog.askopenfile(mode="r")
and use
command = foo
In the Button call. What you're doing in your code above corresponds to command = foo() instead (which executes the function), and not command = foo.
If you want to do everything in the same line, and not define an extra function, you could also use a lambda and write:
command = lambda: filedialog.askopenfile(mode="r")

Related

tkinter multi threading auto start [duplicate]

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.
So in my tkinter app I need to check input on button but when I start the program with this code it starts the function (without me clicking the button) and I have no idea why it does that is.
submit = tk.Button(app,text='Submit details',bg='black',fg='white',
command=threading.Thread(target=get_input_info).start()).grid(row=4)
You need to remove Parenthesis -
command=threading.Thread(target=get_input_info).start
Or use, lambda (useful when you need to pass args) -
command=lambda:threading.Thread(target=get_input_info).start()
Just remove the pair of parantheis from the end of the command argument.
eg:
from tkinter import *
import threading
def hehe():
print("some stuff")
win=Tk()
submit = Button(text="something", command=threading.Thread(target=hehe).start).pack()
win.mainloop()

Call a function after pressing a button (tkinter) [duplicate]

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.
I created a button "button" and I want to call a function "function" when I press on this button.
When I run the program, the function is called and then the button appears. If I press it once again, the function is not called. So how to disable calling the function without pressing the button and how to enable calling the function at each press on the butotn?
from tkinter import *
def function():
what this function does
root = Tk()
button = Button(root,text="Call function", command=function())
button.pack()
root.mainloop()
You need to pass the function (don't call it):
button = Button(root,text="Call function", command=function)
I think you have to remove the brackets from the function, because you just want to give the function as a parameter to your button and you do not want to call the function instead.
So it will look like:
button = Button(root,text="Call function", command=function)

Python gui button (tkinter) function is being run when program is executed and not onclick [duplicate]

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.
i'm currently learning python/tkinter and I have two buttons working correctly but my final button is assigned to a function, which is supposed to run on click but it instead runs when the program is executed and then will not run when the button is clicked. I have looked at a couple of other threads and cannot work out why mine wont work.
Thanks a lot
def onclickSelectSheet():
test()
buttonSelectSheet = Button(topFrame, text="Select an information sheet", command=onclickSelectSheet(), bg = '#CDCDCD')
buttonSelectSheet.place(relx=0.2, rely=0.1, relwidth=0.6, relheight=0.4)
To execute a Button-command on click, you need to set the command option as follows:
Button(..., command=lambda: onclickSelectSheet(),...) # with parenthesis
OR
Button(..., command=onclickSelectSheet,...) # without parenthesis
Using parenthesis () without lambda executes the function as soon as the Button widget is defined

Python 3.5.1, Tkinter: Functions execute on start instead of button click [duplicate]

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 6 months ago.
So, I'm creating a client manager software for a local club.
I'm using Python 3.5.1 and Tkinter.
Used a Notebook to nest my Frames.
On my first frame I made the form to add new clients (labels and textboxes) and an "add" button at the end.
Problem is that it executes the function associated with the button insted of onclick, and the button actually does nothing on click.
Been searching everywhere and it seems a rare problem.
Help?
From what I could decipher, as stated in comments your not setting the command properly.
If you have a function you need to set my_button = tk.Button(..., command = my_function)
If your function takes a keyword argument then you need to pass the function like so
my_button = tk.Button(...., command = lambda: function(argument))
I would try using lambda: before the command.
For instance, replace readFile(file) with lambda: readFile(file).
This will ensure an anonymous ("lambda") function with no parameters is passed, which upon execution will run the intended code. Otherwise, the function is executed once when the behavior is set, then the returned value is simply re-evaluated every time rather than the appropriate function being called.

How to have parameters to a button command in tkinter python3 [duplicate]

This question already has answers here:
Python Tkinter: Passing arguments to Button widget
(1 answer)
commands in tkinter when to use lambda and callbacks
(2 answers)
Closed 7 years ago.
I want to have UI where when I press a button, stuff pops up in the console. The issue is the stuff prints into the console before I press the button. After some testing, I have found that if I don't use parenthesis to call my function in the command argument, it works fine.
ex: A function called hello prints hello world in the console, so I would call it like button=Button(master, command=hello) instead of button=Button(master, command=hello())
The issue with this way is I can't use parameters.
here is an example of a code similar to mine:
index={'Food':['apple', 'orange'], 'Drink':['juice', 'water', 'soda']}
Names=['Food', 'Drink']
def display(list):
for item in list:
print(item)
from tkinter import *
mon=Tk()
app=Frame(mon)
app.grid()
for item in Names:
button=Button(mon, text=item, command=diplay(index[item]))
button.grid()
mon.mainloop()
Any ideas of how to be able to use parameters? I hope this all made sense, but if it didn't please leave a comment. Thank You.
You are looking for the lambda keyword:
from tkinter import *
index={'Food':['apple', 'orange'], 'Drink':['juice', 'water', 'soda']}
Names=['Food', 'Drink']
def display(list):
for item in list:
print(item)
mon=Tk()
app=Frame(mon)
app.grid()
for item in Names:
Button(mon, text=item, command= lambda name = item: display(index[name])).grid()
mon.mainloop()
You have to use name = item so that every time a button is initialized, it takes the current value of item from the for loop.
If for example you used lambda: display(index[item]), both buttons would display the values for 'Drink' because that is the last value of the lambda function initialized in the loop.

Categories