Tkinter: disable a item in a Menubar

Greg McFarlane gregm at iname.com
Mon Jun 12 09:36:45 EDT 2000


It is possible to do what you are trying to do.  To work out how to do
it, you need to carefully study the Tkinter and Pmw reference manuals. 
There you will discover that:

    1) The Pmw.MenuBar addmenu() method creates a menu component.
       In this case, the name of the menu component is 'FILE-menu'.
    2) Pmw allows you to access the components of a megawidget, using
       the component() method.
    3) To configure a Tk menu item, use the entryconfigure() method.
    4) To disable a Tk menu item, set the 'state' option to 'disabled'
       and to enable it, set it to 'normal'.

Putting this all together:

   def open_file(self):
      ## change the state of 'open' to DISABLED
      fileMenu = self.menuBar.component('FILE-menu')
      fileMenu.entryconfigure('open', state = 'disabled')

   def save_file(self):
      ## change the state of 'save' to DISABLED and 'open' to Normal
      fileMenu = self.menuBar.component('FILE-menu')
      fileMenu.entryconfigure('open', state = 'normal')
      fileMenu.entryconfigure('save', state = 'disabled')

Greg

On 7 Jun, Klaus Bruns wrote:
> Hi all,
> 
> I want to change the state of an item in a Menubar, but I don't now how
> to do.
> For example: in the Menu 'File' there is a funktion to open and save a
> file. After using the 'open' funktion this funktion is disable till
> using the 'save' funktion.
> 
> I use Pmw for creating the Menubar:
> 
> import Pmw
> from Tkinter import *
> 
> class mymenu:
>    def __init__(root):
> 	Pmw.initialise(root)
> 	self.balloon=Pmw.Balloon(root)
> 	self.menuBar=Pmw.MenuBar(root,hull_relief=RAISED,hull_borderwidth=1,
>                                  balloon=self.balloon)
>         self.menuBar.pack(fill=X)
>         self.menuBar.addmenu('FILE','open and save files')
> 	self.menuBar.addmenuitem('FILE','command','open a file',label='open',
> 				  command=self.open_file)
> 	self.menuBar.addmenuitem('FILE','command',save a file',label='save',
> 				  command=self.save_file)
> 	
>    def open_file(self):
> 	file  = open(filename)
> 	## here I want to change the state of 'open' to DISABLED
> 
>    def save_file(self):
> 	## do something to save the file
> 	## and change the state of 'save' to DISABLED and 'open' to Normal
> 
> root = TK()
> mymenu(root)
> root.mainloop()
> 
> Can anyone help me?
> 
> 	Klaus
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 

-- 
Greg McFarlane     INMS Telstra Australia     gregm at iname.com




More information about the Python-list mailing list