Re: Pulldown Menu Entry Callbacks.

Guido.van.Rossum@cwi.nl
Fri, 20 Jan 1995 14:46:37 +0100

> Does anybody know how to add arbitrary data to pulldown-menu entry
> callbacks? Suppose I want to have several menu-entries all using the
> same callback but in the callback I wish to find out some unique information
> about the item generating the callback. A simple case would be to read
> a file and create a menu-entry for each line in the file. When any entry
> is chosen, the callback simply prints the text from the menu-entry.
>
> Since you won't know the number of entries in the file before hand, you
> can't write a unique callback for each item.

The simplest solution would seem to be to create a little class that
serves as a container for the per-item data, create an instance for
each item, and use a method of the instance as a callback. For
example:

class PerItem:

def __init__(self, per_item_data):
self.per_item_data = per_item_data

def callback(self, *args):
print self.per_item_data

for line in open(filename).readlines():
per_item = PerItem(line)
b = (...create the menu item...)
b['command'] = per_item.callback

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>