472,811 Members | 4,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 5856
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
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
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
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
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
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
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
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
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
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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.