tkinter menuitem question

G. Willoughby sab at NOSPAM.freeuk.com
Wed Mar 20 19:28:28 EST 2002


I've just done this tonight with this code:
[snip]
class GenericCallback:
 def __init__(self, callback, *firstArgs):
  self.__callback = callback
  self.__firstArgs = firstArgs
 def __call__(self, *lastArgs):
  apply(self.__callback, self.__firstArgs + lastArgs)
[/snip]
[snip]
  self.zone=Zones("D:\DAoC")
  self.zonemenu = Menu(self.menubar, tearoff=0)

  self.albionmenu=Menu(self.menubar, tearoff=0)
  self.midgardmenu=Menu(self.menubar, tearoff=0)
  self.hibernianmenu=Menu(self.menubar, tearoff=0)

  for x in range(len(self.zone.locations)):
   menuName=self.zone.locations[x][0]+" : "+self.zone.locations[x][1]
   zoneNumber=int(self.zone.locations[x][0])

   if zoneNumber<100:
    self.albionmenu.add_command(label=menuName,
command=GenericCallback(self.zoneSelected, zoneNumber))
   elif zoneNumber>100 and zoneNumber<200:
    self.hibernianmenu.add_command(label=menuName,
command=GenericCallback(self.zoneSelected, zoneNumber))
   elif zoneNumber>200 and zoneNumber<300:
    self.midgardmenu.add_command(label=menuName,
command=GenericCallback(self.zoneSelected, zoneNumber))

  self.zonemenu.add_cascade(label="Albion", menu=self.albionmenu)
  self.zonemenu.add_cascade(label="Midgard", menu=self.midgardmenu)
  self.zonemenu.add_cascade(label="Hibernia", menu=self.hibernianmenu)
  self.menubar.add_cascade(label="Zone", menu=self.zonemenu)
[/snip]
[snip]
 # Zone menu commands
 def zoneSelected(self, zone):
  print zone
[/snip]

make sense?

G. Willoughby

"Ken Guest" <kwg at renre-europe.com> wrote in message
news:mailman.1016634429.30440.python-list at python.org...
> ok, I might be way off here but is there a way with a collection of
> Tkinter menuitems that all have their callbacks set to be the one
> function to determine which menuitem was clicked on?
>
> for example, given the snippet:
>
>
>         menubar1=Pmw.MenuBar(vbox1)
>         menubar1.addmenu('TopLvlMenu','this is a tooltip')
>         menubar1.addcascademenu('TopLvlMenu', 'Item#6','tool tip for
> item 6')
>         menubar1.addmenuitem('Item#6', 'command', '',
> label="item#7",underline=0, command=menu_item_selected)
>         menubar1.addmenuitem('Item#6', 'command', 'oct', label="item#8",
>         underline=0, command=menu_item_selected)
>         menubar1.addmenuitem('TopLvlMenu', 'command', 'naoi',
> label="item#9", underline=0, command=menu_item_selected)
>         menubar1.addmenuitem('TopLvlMenu', 'separator', '')
>         menubar1.addmenuitem('TopLvlMenu', 'command', '',
> label="item10", underline=0, command=menu_item_selected)
>         menubar1.pack(expand=NO, side=TOP)
>
>
> Is it possible to give these menuitems the one handler function/callback
> function that can determine which item was clicked on?
> The reason for asking is that I have a dynamically generated cascading
> popup menu and need to know which menuitem has been selected.
>
> I have thought of displaying a very small dialog box which would only
> contain a combobox and nothing else. the dialog box would be unloaded
> once the user selects an item from the combobox but this wouldn't really
> work as there need to be cascaded entries.
>
> any ideas?
>
> k.
>
>
>





More information about the Python-list mailing list