Pmw menubutton enable/disable, better answer ..

Camilo Olarte colarte at telesat.com.co
Mon Sep 22 02:05:30 EDT 2003


 Well i needed to do something like that , so i came up with this example .
 the main idea here is using the entryconfig with an index that can be the same 
 as the label you gave to the menubutton
 Hope it helps ...

<Python Code> 
!/usr/bin/python
#tkex1.py - tkDemo#1 1.1 - 30 May 01
#A Windows/Unix script to demonstrate basic Tkinter programming
#Update at http://www.3dartist.com/WP/python/tknotes.htm#tkex1
#Created by Bill Allen <dempy at 3dartist.com> --Modified by Camilo Olarte .9/22/2003

usingIDLE = 0                    #set = 1 if running from IDLE, otherwise 0
import tkMessageBox
import Tkinter as tk
#--- classes ---
class uiClass:                                  #user interface class
    def __init__(self,master,ar,xy,flex):       #  used once for main GUI
	self.master = master
        menubar = tk.Menu(master)               #create a menu object tied
        master.config(menu=menubar)             #  to the root parent
        self.mainmenu = tk.Menu(menubar,tearoff=0)   #create a dropdown menu
	self.mainmenu.add_command(label='Exit',underline=1,command=self.quit)
	self.mainmenu.add_command(label='Do Something',underline=1,command=self.fnDoSomething)
	self.mainmenu.add_command(label='Kill Exit :) ',underline=1,command=self.fnDeactivateMenu)
	menubar.add_cascade(label='Main',       #give the dropdown menu a
                            menu=self.mainmenu,      #  name & add it to the 
                            underline=0)        #  menu object
        self.frame = tk.Toplevel(relief='ridge', #border style
                                 borderwidth=2, #create the main GUI window
                                 menu=menubar)  #  with its menu object
        self.frame.geometry(ar+xy)              #set its size & location
        self.frame.resizable(flex,flex)         #(dis)allow resize horiz/vert
        if not usingIDLE:                       #system window exit protocol
            self.frame.protocol('WM_DELETE_WINDOW',self.quit)
        #end def self.__init__ 
    def fnDoSomething(self):
	tkMessageBox.showinfo('!!','Doin Somethin!:')

    def quit(self):
        if usingIDLE:
            self.frame.destroy()
        else:
            self.frame.quit()
            root.quit()

    def fnDeactivateMenu(self):
	self.mainmenu.entryconfig(self.mainmenu.index("Exit"), state=tk.DISABLED)
    #end define uiClass
#--- do some work ---
if __name__ == '__main__':              #optional, but good practice
    root = tk.Tk()
    root.title('tkDemo#1')
    root.withdraw()                     #suppress unwanted window
    mainUI = uiClass(root,              #set up the main GUI window
                     '400x200',         #  width & height
                     '+20+20'           #  initial XY screen location
                     ,0)                #  resizing turned off
    if not usingIDLE:                   #avoid IDLE or other Tkinter IDEs
        root.mainloop()                 #outer event loop

</Python Code> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tkex1.py
Type: application/octet-stream
Size: 2814 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030922/c0af89f4/attachment.obj>


More information about the Python-list mailing list