Python Forum
[Tkinter] Button 'command' Argument Confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button 'command' Argument Confusion
#1
I am confused by the 'command' argument for buttons. I have the following button defined:
submitButton=tk.Button(top,text='Submit',command=submitData(top)).grid(row=rw,column=2,pady=5,sticky=tk.W)
The command (submitData) is being executed immediately upon display before the button is clicked. The when I click the button, nothing is executed. What am I doing wrong? TIA
Reply
#2
You are supposed to pass something callable to command, when the button is pressed it would then do the call,
You are actually passing something that is already being called the result of this call is being passed to the command.
Create a function/method that will do the actions you want to happen when a button is pressed, pass that uncalled function to command.
Reply
#3
This code makes a button and returns a handle to the button.
submitButton=tk.Button(top,text='Submit',command=submitData(top))
This code tells the grid layout in the parent window (top) how it should layout the new button. .grid() returns None.
submitButton.grid(row=rw,column=2,pady=5,sticky=tk.W)
When you string the two commands together you get a new button in the right spot, but the value in submitButton is None, the return value of the .grid() calll.
Reply
#4
Thanks for the replies but that is exactly what I am doing. The subroutine 'submitData' exists and is not called anywhere else (one and only one reference). That is the confusing part. How is it getting called if not from the button and even if it is, why does clicking the button not call it again?

As for what the button returns, I am not using that reference anywhere so it doesn't matter. Indeed I did even equate it to a variable.
Reply
#5
submitData is being called with the argument top
submitData(top)
to have a button do the call you would have a function or method here is an example with a function

def call_me():
    print("I was Called")
command would be passed call_me without the () so when the button is clicked it would do the calling
command=call_me
not
command=call_me()

See the following link to another forum post for an example of using partial to send values to a function/method on a button click
https://python-forum.io/thread-28552-pos...#pid121468
Reply
#6
Thanks. Then how do I pass an argument to the command?

The bottom line is I want to process the entry widgets in 'top' when the button is clicked. That means I have to pass the parent to the subroutine to extract the data, right?
Reply
#7
I already added a link of how to do that at the end of my previous post.
Reply
#8
Sorry, I missed that. I was not aware of 'partial'. Some of this is hard to get from the documentation. Kind of like looking up how to spell a word in the dictionary. You have to know how to spell it before you can look it up. Thanks.
Reply
#9
Your code does this:
tempValue = submitData(top)
tempButton = tk.Button(top,text='Submit',command=tempValue)
submitButton = tempButton.grid(row=rw,column=2,pady=5,sticky=tk.W)
This ends up setting submitButton=None and probably binds the button command=None.

To specify an argument in the command callback you can use a lambda expression or a partial function. This is your code fixed using a lambda expression.
submitButton = tk.Button(top, text='Submit', command=lambda: submitData(top))
submitButton.grid(row=rw, column=2, pady=5, sticky=tk.W)
The lambda expression creates a function that looks something like this:
def unnamed_function():
    submitData(top)
And binding command to the lambda expression does something like this:
submitButton = tk.Button(top, text='Submit', command=unnamed_function)
So command is bound to a function, not a function result, and the function you want to call is not called until the lambda expression is evaluated.
Reply
#10
OK, so I can use lambda rather than partial to get to the same place. Thus I wouldn't need to include another library. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax Error: Positional argument follows keyword argument Rama02 3 4,287 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  Get name of command button Heyjoe 3 2,448 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 2,405 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 3,058 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 5,670 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 2,550 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  tkinter button not accessing the command when clicked jhf2 1 3,771 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,165 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] How to add an argument to a buttons command call petterg 2 3,242 Jun-20-2019, 09:05 PM
Last Post: petterg
  [Tkinter] command button automaticaly fired kferhat 2 2,620 Jun-16-2019, 08:21 PM
Last Post: kferhat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020