[Tutor] Layering Canvas widgets with Tkinter
Phillip Hart
phillip.hart at gmail.com
Mon Jun 20 06:46:00 CEST 2005
Greetings again helpful friends,
I've written a series of Python/Tkinter programs, each of which will
represent a different menu/screen in a simple Tkinter game I'm making. Now,
my trouble is linking them together through layered menus with buttons.
So far, I've tried using a single Tk window, creating a series of canvas
widgets, each containing a series of buttons. When pressed, each button
calls a function which uses .grid_remove() on the current canvas and creates
a new canvas with new buttons in its place (a new menu).
My trouble is the menu layers are not covering each other up correctly. menu1
and menu2 functions fail to create a canvas of their designated size and
.grid_remove() does not seem to remove the main canvas, thus creating the
appearance of jumbled, stacked buttons.
Oddly, the menu3 function will create its designated canvas to cover up the
main menu, but only seems to work because it has no buttons. Likewise, the
mainmenu function will return the menu cleanly to its normal state.
I've also fiddled with .grid_forget() and .destroy() and had the same
results.
I'm still very inexperienced. Can someone give me a hand here?
Thanks,
-Phil
#########
from Tkinter import *
def menu(y):
main.grid_remove ()
root.update()
if y ==1:
menu1()
if y==2:
menu2()
if y==3:
menu3()
def menu1():
X=Canvas(root, width=200, height=200, bg="blue")
X.grid(row=0,column=0)
but2=Button(X,text="back",command=mainmenu)
but2.grid()
def menu2():
Y=Canvas(root, width=200, height=200, bg="red")
Y.grid(row=0,column=0)
but1=Button(Y,text="back",command=mainmenu)
but1.grid()
def menu3():
Z=Canvas(root, width=200, height=200, bg="green")
Z.grid(row=0,column=0)
def mainmenu():
main=Canvas(root,width=200,height=200,bg="grey")
main.grid(row=0,column=0)
men1=Button(main,text="menu 1",command=lambda y=1:menu(y))
men1.grid()
men2=Button(main,text="menu 2",command=lambda y=2:menu(y))
men2.grid()
men3=Button(main,text="menu 3",command=lambda y=3:menu(y))
men3.grid()
root= Tk()
root.geometry('200x200')
main=Canvas(root,width=200,height=200,bg="grey")
main.grid(row=0,column=0)
men1=Button(main,text="menu 1",command=menu1)
men1.grid()
men2=Button(main,text="menu 2",command=menu2)
men2.grid()
men3=Button(main,text="menu 3",command=menu3)
men3.grid()
root.mainloop()
#################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050619/83d80f60/attachment.htm
More information about the Tutor
mailing list