Re: Tkinter menu question--how to pass arguments



kyosohma@xxxxxxxxx wrote:
On Mar 30, 2:32 pm, Kevin Walzer <k...@xxxxxxxxxxxxxxx> wrote:
I'm having difficulty structuring a Tkinter menu entry. Here is the
command in question:

self.finkmenu.add_command(label='Update List of Packages',
command=self.authorizeCommand(self.scanPackages))

When I start my program, it crashes because it's trying to run the
command self.authorizeCommand. The reason I'm structuring it in this
fashion is that this command takes another command as an argument--in
this case, self.ScanPackages.

The basic structure of the program is that the self.authorizeCommand
function pops up a dialog box for a password; it then feeds the password
to the function that it takes as an argument, i.e. self.scanPackages.

I tried setting up the menu entry without the additional parameter, i.e.
command=self.authorizeCommand, but then when I try to run the command
from the menu, it complains there are not enough arguments.
Unsurprising, since self.authorizeCommand takes another function name as
an argument.

How can I structure the menu item to reflect the correct number of
arguments without it trying to execute the command?

--
Kevin Walzer
Code by Kevinhttp://www.codebykevin.com

There are various ways to accomplish this. The 2 most popular that I
am aware of are using a helper function or lambda.

using a lambda:

command=(lambda:self.authorizeCommand(self.scanPackages))


using a handler (i.e. indirection layer):

def func():
self.authorizeCommand(self.scanPackages)

self.finkmenu.add_command(label='Update List of
Packages',command=func)


Both of these are talked about in detail in "Programming Python 3rd
Ed" by Lutz. I found that helpful for me. Of course, I decided to stop
using Tkinter and switched to wxPython. Hope this gets you going
though.

Mike

lambda does the trick--thanks. I have the Lutz book but overlooked that part.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
.



Relevant Pages

  • Re: TIP #187: Procedures as Values
    ... Actually in a real implementation of the TIP to add the [lambda] ... command in the core can be a good idea, ... >> an anonymous procedure before to perform any other lookup. ... > to avoid the conversion to string that would happen just to find ...
    (comp.lang.tcl)
  • Re: A different design for closures
    ... >> Storing these values in a namespace feels wrong to me. ... >> stored such that it's part of the command data. ... > The value stored in the scope variable is also used as the scope for the ... the lambda would change the lambda, not the values fed to it in its ...
    (comp.lang.tcl)
  • Re: lambda... again
    ... you feel there are performance problems with then supply ... This wat, the lambda expression itself gets generated, ... The solution is to allow using lambdas as a command name. ... % apply {args showCaller} can I see this? ...
    (comp.lang.tcl)
  • Re: TIP #187: Procedures as Values
    ... Leaving aside that with the proposed alias "lambda", ... to avoid the conversion to string that would happen just to find ... If real lists ... the command could be checked ...
    (comp.lang.tcl)
  • Re: lambda... again
    ... Transparent lambda calls should have been built-in, ... the rationale behind the command. ... built-in commands, global procs, package procs, namespaces, aliases.. ...
    (comp.lang.tcl)