[Tkinter-discuss] Help on subclassing - Tkinter.Menu

thicket mark_eastwood at ntlworld.com
Sun Sep 13 03:34:09 CEST 2009

Hi  - 

I'm building menus based on data from a configuration file - to date I've
created standard python classes to represent the menu data then used Tkinter
to retrieve the information and create the menu widgets. So I thought why
not take it a step further and combine the two - sample test code below

#!/usr/bin/env python
from Tkinter import *

class MyMenu(Menu):
        def __init__(self,parent):
                Menu.__init__(self,parent, tearoff=0)

        def my_add(self):
                self.add_command(label=self.label1, command=root.quit)                        
# did not expect this to work
                Menu.add_command(self,label=self.label2, command=root.quit)               
# this I thought may work

root = Tk()
menubar = Menu(root)
menubar.add_cascade(label="Test", menu=menu)


What I would like to do is not hardcode the 'tearoff=0' in the parent class
constructor i.e. instantiate MyMenu by
menu=MyMenu(menubar, tearoff=0)

Also not 100% sure why both add_command() statements in my_add() work?
View this message in context: http://www.nabble.com/Help-on-subclassing---Tkinter.Menu-tp25419747p25419747.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.

More information about the Tkinter-discuss mailing list