This can be done using a lambda, like so:
button = Tk.Button(master=frame, text='press', command= lambda: action(someNumber))
This is a simple way to bind the argument without an explicit wrapper method or modifying the original action.
This can be done using a lambda, like so:
button = Tk.Button(master=frame, text='press', command= lambda: action(someNumber))
This is a simple way to bind the argument without an explicit wrapper method or modifying the original action.
This can also be done by using partial from the standard library functools, like this:
from functools import partial
#(...)
action_with_arg = partial(action, arg)
button = Tk.Button(master=frame, text='press', command=action_with_arg)
How to use a Tkinter button (call a function) that has an argument?
How to call same function with different argument given from list in tkinter button command option
Buttons with same parameters
Tkinter button command using lambda to call a class method - how???
Videos
Hello everyone,
I have a Tkinter window that is defined in a class, and I have a function that gets a value from the Tkinter window.
I.E.
class newTopLevel(object):
def __init__(self, root):
self.newWindow = Toplevel(root)
entry_1 = tk.Entry(self.newWindow).grid(row=0, column=1)
self.newWindow.btn = tk.Button(self.newWindow,text='Enter',command=self.fun)
self.newWindow.btn.grid(row=0,column=2)
def fun(self):
get_value=entry_1.get()The issue here is entry_1 is not defined in my function. I normally don't have my Tkinter within a class, thus its a global and its properties (such as buttons and entry grids) are globals and they don't have to be defined. However in this case, now it does need to defined. I tried this:
def fun(self,entry_1):
get_value=entry_1.get()However, then I cannot use the button command to call it.
self.newWindow.btn = tk.Button(self.newWindow,text='Enter',command=self.fun(entry_1))
If I don't include the argument entry_1, then I'll get an error that fun has an argument and I haven't given any when using the button. I don't quite know how to use a Tkinter button that has arguments. Any help would be greatly appreciated!
Happened to just stumble across this sub while looking for an answer that I can not find. Hopefully I can get an answer.
I've been working with python on and off for a while now. Trying to make a program with Tkinter. I have multiple buttons that I'm trying to place but I want all the parameters to be the same.
The code looks like this (don't post often so not sure how to do the code thing)
params= { 'font'= ('Courier', 10), 'width'= 15, 'height'= 5, }
tk.button(text='new button', command, params)
I know usually when passing multiple parameters I can just do params = params and all is good. Its not working with the buttons. Anything I look up tells me how to do multiple commands or multiple arguments in the command. Am I on the right track or am I missing something?