Tkinter menu question--how to pass arguments

K

Kevin Walzer

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?
 
D

Dave Opstad

Kevin Walzer said:
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?

If self.scanPackages exists as an attribute of self, why do you need to
pass it in? If your command is just self.authorizeCommand, and that
method makes use of self.scanPackages when it runs, then it all should
work without your having to specify it here.

Dave
 
K

kyosohma

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?

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
 
K

kyosohma

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?

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
 
K

Kevin Walzer

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.
 
K

Kevin Walzer

Dave said:
If self.scanPackages exists as an attribute of self, why do you need to
pass it in? If your command is just self.authorizeCommand, and that
method makes use of self.scanPackages when it runs, then it all should
work without your having to specify it here.

Dave
self.AuthorizeCommand is a generic dialog to feed a password to several
different commands. So, the specific function needs to be specified as a
parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top