473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Tkinter menu question--how to pass arguments

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 Kevin
http://www.codebykevin.com
Mar 30 '07 #1
5 2537
In article <85***************************@FUSE.NET>,
Kevin Walzer <kw@codebykevin.comwrote:
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
Mar 30 '07 #2
On Mar 30, 2:32 pm, Kevin Walzer <k...@codebykevin.comwrote:
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.scanPac kages))
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

Mar 30 '07 #3
On Mar 30, 2:32 pm, Kevin Walzer <k...@codebykevin.comwrote:
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.scanPac kages))
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

Mar 30 '07 #4
ky******@gmail.com wrote:
On Mar 30, 2:32 pm, Kevin Walzer <k...@codebykevin.comwrote:
>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.scanPac kages))
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
Mar 30 '07 #5
Dave Opstad wrote:
In article <85***************************@FUSE.NET>,
Kevin Walzer <kw@codebykevin.comwrote:
>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
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.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: Adonis | last post by:
I wish to create a popup menu from a list, when it is created they all show the same label from the list: Months = def _Menu_Click(self, month): print month menu = Menu(fraMain, tearoff=0)
3
by: John Pote | last post by:
I have a menu bar in a top level window as follows menuBar = Menu(rootWin) menuBar.add_command( label='Quit', command=rootWin.quit) configMenu = Menu(menuBar, tearoff=0)...
15
by: Prachi Dimble | last post by:
Hi, In vb.Net one can pass arguments to properties. How does one achieve it in c#? Given below is the vb.net code for passing arguments to property getters and setters.. Thanks, Prachi ...
2
by: gh14tq5 | last post by:
Hi, I'm writing a small GUI program in Python/Tkinter (my first Python program). I want to make a menu which lists the names of a number of text files that my program uses/generates. When I...
2
by: Dustan | last post by:
I don't know if this is because of Tkinter (ie Tk) itself or the Windows default way of handling things, but when I create a very long menu (my test is shown below), the way it displays is rather...
1
by: megh55555 | last post by:
Hi , how can we pass arguments to a function located in xyz.py file using the DOS command Prompt. I know its very easy to pass using the python command line but m finding it impossible to do...
2
by: loopz123 | last post by:
Hi all, How can i pass arguments while calling other application from vb.
3
by: MartinRinehart | last post by:
Tkinter definitely deserves more respect! I'm making rapid progress and it looks good. But am stuck on this: I want the File/Save state to change from disabled to enabled, depending on whether...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.