tkinter menu bars, assigning children to parents, and grid/pack managers

Josh pushpin at
Wed Jul 30 19:41:37 CEST 2003

Caution, newbie approaching...

I'm trying to come up with a very simple Tkinter test application that
consists of a window with a drop-down menu bar at the top and a grid
of colored rectangles filling the remainder of the window.  Mind you,
this is a contrived test application to help me understand Tkinter and
Python, not an actual application yet.  I've trivially subclassed
Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases to a
trivial subclass of Tkinter.Frame called MultiColor, and then tried
adding a MultiColor and a subclass of Tkinter.Frame called
MultiColorMenu to a root window.  The problem is that the menu bar is
overlaid on top of the MultiColor gridded frame, instead of above it. 
I think the problem is that I don't know how to assign a widget as a
child to a parent widget. The code I'm talking about is copied below. 
Any ideas?


## Begin code.

import Tkinter

class ColorCanvas(Tkinter.Canvas):
    "This class is simply a colored rectangle."
    def __init__(self, background, numXPixels=300, numYPixels=100,
        ## Initialize variables.
        if master is None:
            master = self
        self.numXPixels = numXPixels
        self.numYPixels = numYPixels
        Tkinter.Canvas.__init__(master, background=background,
width=self.numXPixels, height=self.numYPixels, borderwidth=0)

class MultiColor(Tkinter.Frame):
    "This class is a grid of colored rectangles."
    def __init__(self, numOfColors, master=None):
        Tkinter.Frame.__init__(self, master)
        self.colors = []
        for i in range(numOfColors):
            self.colors[i].grid(row=i/3, column=i%3)

class MultiColorMenu(Tkinter.Frame):
    "This class is a bar of drop-down menus for manipulating a grid of
colored rectangles."
    def __init__(self, master=None):
        Tkinter.Frame.__init__(self, master)

    def help_menu(self):
        help_btn = Tkinter.Menubutton(self, text='Help', underline=0)
        help_btn.pack(side=Tkinter.LEFT, padx="2m") = Tkinter.Menu(help_btn)"How To", underline=0,
command=self.helpFoo)"About", underline=0,
        help_btn['menu'] =
        return help_btn

    def helpFoo(self):
        print 'Heelllppppp!!!'

def main():
    root = Tkinter.Tk()
    root.title('Lifton\'s Fabulous Color Canvases')
    mc = MultiColor(numOfColors=22, master=root)
    #mc.pack(fill=Tkinter.BOTH, side=Tkinter.BOTTOM)
    mc.grid(row=1, column=0)
    mc_menus = MultiColorMenu(master=root)
    #mc_menus.pack(fill=Tkinter.X, side=Tkinter.TOP)
    mc_menus.grid(row=0, column=1)


## End code.

More information about the Python-list mailing list