467,864 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,864 developers. It's quick & easy.

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

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[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)
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.
Jul 18 '05 #1
  • viewed: 5534
Share:
1 Reply
Josh wrote:
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=menuri cerca)

# -- 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.

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Gregory | last post: by
1 post views Thread by syed_saqib_ali | last post: by
6 posts views Thread by Richard Lewis | last post: by
reply views Thread by syed_saqib_ali | last post: by
reply views Thread by Stewart Midwinter | last post: by
5 posts views Thread by H J van Rooyen | last post: by
reply views Thread by Svenn Bjerkem | last post: by
4 posts views Thread by Simon Forman | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.