[Tutor] Tk_help

Michael P. Reilly arcege@speakeasy.net
Sat, 12 Jan 2002 16:04:59 -0500


On Sat, Jan 12, 2002 at 12:48:33PM -0500, Karshi wrote:
> I have the following code in my program:
> --------------------------------------
> class MyFrame(Frame):
>     def __init__(self,parent=None ):
>         Frame.__init__(self, parent,relief=RAISED, borderwidth=2)
>         self.pack(fill=X)
>         Command =  makeCommandMenu(self)
> 
>         self.tk_menuBar(Command)
>     def func(self):
>         print " This will keep printing...."
> 
> def makeCommandMenu(self):
>     
>     Command =  Menubutton(self, text= ' Buttons', underline=0)
>     Command.pack(side=LEFT, padx="2m")
>     Command.menu = Menu(Command)
> 
>     Command.menu.add_command(label="Undo")
>     Command.menu.entryconfig(0, state=DISABLED)
> 
>     Command.menu.add_command(label="New...", underline=0, command=self.func )
>     Command.menu.add_command(label="Open...", underline=0)
> 
>     Command.menu.add('separator')
>     Command.menu.add_command(label='Quit', background='white', 
> activebackground='green', command=Command.quit)
> 
>     Command['menu'] =  Command.menu
>     return Command
> --------------------------------------
> which works fine.
> I am wondering  about the line:
> "Command['menu'] =  Command.menu"
> Can you explain me what  this command does?
> Thanks

It sounds confusing, but with Tkinter widgets, the statement
  widget['name'] = value
is the equivalent to
  widget.config(name = value)

A new Menu widget is created, and you need to tell the Menubutton what the
pop-up menu is through either 'widget["menu"]' or 'widget.config(menu=)'.
The new Menu widget also happens to be stored as an attribute of the
Menubutton with 'widget.attr = value' (in this case 'Command.menu =
Menu(Command)').

It might help if you change it to:
  Command.menu_subwidget = Menu(Command)
  ...
  Command.config(menu = Command.menu_subwidget)

Better? :)

  -Arcege