473,503 Members | 9,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
1 5958
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7182
by: Andrew Gregory | last post by:
Could someone help me out with these few lines of code: I would like to know why the Quit button in this application removes the buttons and causes "Quitting" to be printed, but does not close the...
1
2952
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
6
8235
by: Richard Lewis | last post by:
Hi there, I've got a tree control in Tkinter (using the ESRF Tree module) but I can't get it to layout how I want it. I'd like to have it so that it streches north/south (anchored to the top...
0
3565
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
0
2338
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
5
3518
by: H J van Rooyen | last post by:
Hi, I am struggling to get the pack method to do what I intend. I am trying to display user input in a seperate window, along with a little description of the field, something like this: ...
0
1414
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and...
4
4149
by: Simon Forman | last post by:
Hi all, I realize this is more of a Tk question than a python one, but since I'm using python and don't know Tcl/Tk I figured I'd ask here first before bugging the Tcl folks. I am having a...
1
1685
by: Francesco Bochicchio | last post by:
Il Mon, 18 Aug 2008 12:15:10 +0100, dudeja.rajat ha scritto: Uhm, I don't think you should use the grid manager to obtain a window like that. The grid manager is for equally distributing...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.