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

J

Josh

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?

Thanks,
josh

## Begin code.

import Tkinter

class ColorCanvas(Tkinter.Canvas):
"This class is simply a colored rectangle."
def __init__(self, background, numXPixels=300, numYPixels=100,
master=None):
## 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.append(ColorCanvas(background='purple'))
self.colors.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)
self.tk_menuBar(self.help_menu())

def help_menu(self):
help_btn = Tkinter.Menubutton(self, text='Help', underline=0)
help_btn.pack(side=Tkinter.LEFT, padx="2m")
help_btn.menu = Tkinter.Menu(help_btn)
help_btn.menu.add_command(label="How To", underline=0,
command=self.helpFoo)
help_btn.menu.add_command(label="About", underline=0,
command=self.helpFoo)
help_btn['menu'] = 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)
root.mainloop()

main()

## End code.
 
F

furliz

Josh said:
Caution, newbie approaching...

well... newbie vs newbie :))
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.

It seems you'd like to obtain a standard window with a standard menubar
attached at the top.... is it right?

Perhaps I'm not the best person for your problem resolution, but I hope
I'll could give you a contribute :)
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)
self.tk_menuBar(self.help_menu())

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

def helpFoo(self):
print 'Heelllppppp!!!'
For my knowledge (..very small on python, yet) you should use the
Tkinter.Menu widget rather than creating an extension of the
Tkinter.Frame like you did in your MultiColorMenu class.

Just copy&paste this simple example, so you see if it is what you're
looking for.

#begin code
from Tkinter import *

root = Tk()
root.title("Menu demo")
root.geometry("300x300")

# -- barra menu
masterMenu = Menu(root)

# -- menu tipo di ricerca
menutiporicerca = Menu(masterMenu,tearoff=0)
menutiporicerca.add_command(label="Ricerca directory")
menutiporicerca.add_command(label="Ricerca file")
menutiporicerca.add_command(label="Ricerca dentro file")

# -- menu ricerche
menuricerca = Menu(masterMenu,tearoff=0)
menuricerca.add_cascade(label="Nuova ricerca...",menu=menutiporicerca)
menuricerca.add_command(label="Salva ricerca")
menuricerca.add_separator()
menuricerca.add_command(label="Esci")
masterMenu.add_cascade(label="Ricerca",menu=menuricerca)

# -- menu info
menuinfo = Menu(masterMenu,tearoff=0)
menuinfo.add_command(label="Info")
menuinfo.add_separator()
menuinfo.add_command(label="About..")
masterMenu.add_cascade(label="?",menu=menuinfo)

#setta barra menu
root.config(menu=masterMenu)
root.mainloop()

#end code

hope this help you. Bye.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top