I making a game that would use multiple canvases drawing on one canvas then switching to another... is there any way to get a new canvas and be able to restore the old canvas with the drawings still on it?
joehms22 18 Junior Poster
What GUI framework are you using? (GTK, QT, WX ...)
Thropian 3 Junior Poster in Training
python, Tkinter
richieking 44 Master Poster
Tkinter not that matured for game. very limited....
TrustyTony 888 pyMod Team Colleague Featured Poster
Here is my menu system, maybe you could adapt it?
from Tkinter import *
import Tkinter as tk
def create_header(t,top):
## Creates the Headings
frame1 = Frame(top, relief=GROOVE, borderwidth=2)
label1 = Label(frame1, text=t, font=('Arial', sqsize/2), fg="red")
label1.pack()
top.create_window(middle, sqsize, window=frame1, anchor=N)
top.pack(fill='both', expand='yes')
def create_buttons(t,place,c, top):
## Creates the Buttons
frame2 = Frame(top, relief=GROOVE, borderwidth=2)
button1 = Button(frame2, text=t, command=c, font=('Arial', middle/16), fg="blue")
button1.pack(side="left")
top.create_window(middle, place, window=frame2, anchor=N)
top.pack(fill='both', expand='yes')
def create_top(frame):
top = Canvas(frame, bg='white', width=s, height=s)
# pick a .gif image file you have in the working directory
# creates the background
image1 = tk.PhotoImage(file="foodimage.gif")
w = image1.width()
h = image1.height()
panel1 = tk.Label(top, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
panel1.image = image1
return top
def create_text(t,top):
frame3 = Frame(top, relief=GROOVE, borderwidth=2)
label2 = Label(frame3, text=t, font=('Arial', middle/16), fg="black")
label2.pack()
top.create_window(middle, sqsize*2, window=frame3, anchor=N)
top.pack(fill='both', expand='yes')
def action():
pass
def create_main():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('Food Ordering System', top)
for t,place,c in (('Help', 3*sqsize, go_help),
('Register as Member', 4*sqsize, go_register),
('Order Food', 5*sqsize, go_food_menu),
('Quit', 7*sqsize, root.destroy),
):
create_buttons(t,place,c,top)
top.pack()
return frame
def create_menu():
## create here all widgets under frame, which is not active (parent None)
frame = Frame(None, borderwidth=2)
top=create_top(frame)
for t,place,c in (('Food Menu', 3*sqsize, go_menu),
('Chefs Specials', 4*sqsize, go_help_text),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame ## return created frame through which can access its widgets
def create_register():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('Register',top)
for t,place,c in (('Submit', 6*sqsize, go_register),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame
def create_food_ordering_system():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('Todays Specials',top)
create_text("""Todays specials are as follows
""",top) #use """ to get space.
for t,place,c in (('Back', 6*sqsize, go_help),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame
def create_about():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('About',top)
for t,place,c in (('Back', 6*sqsize, go_about),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame
def create_register_sucessful():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_text(""" Congradulations Your Registration
has been sucessful""",top)
for t,place,c in (('Back', 6*sqsize, go_register_sucessful),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame
def create_food_menu():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('Menu',top)
for t,place,c in (('Back', 6*sqsize, go_help),
('Main menu', 7*sqsize, go_main),
):
create_buttons(t,place,c,top)
return frame
def create_help():
frame = Frame(None, borderwidth=2)
top=create_top(frame)
create_header('Help',top)
for t,place,c in (('About', 3*sqsize, go_help),
('How do I Register?', 5*sqsize, action),
('Main menu', 7*sqsize, go_main),
## comma makes easier later to put more things here
):
create_buttons(t,place,c, top)
return frame ## must not pack the returned frame
def go_help():
global currentframe
currentframe.pack_forget() ## forget the old currentframe
currentframe=helpframe ## put prepared gameframe instaid
currentframe.pack(fill='both', expand='yes') ## pack it to become visible
def go_register():
global currentframe
currentframe.pack_forget()
currentframe=registerframe
currentframe.pack(fill='both', expand='yes')
def go_menu():
global currentframe
currentframe.pack_forget()
currentframe=menuframe
currentframe.pack(fill='both', expand='yes')
def go_main():
global currentframe
currentframe.pack_forget()
currentframe=mainframe
currentframe.pack(fill='both', expand='yes')
def go_help_text():
global currentframe
currentframe.pack_forget()
currentframe=helptext
currentframe.pack(fill='both', expand='yes')
def go_about():
global currentframe
currentframe.pack_forget()
currentframe=abouttext
currentframe.pack(fill='both', expand='yes')
def go_register_sucessful():
global currentframe
currentframe.pack_forget()
currentframe=register_sucess_text
currentframe.pack(fill='both', expand='yes')
def go_food_menu():
global currentframe
currentframe.pack_forget()
currentframe=food_menu_text
currentframe.pack(fill='both', expand='yes')
def diag_stripe(top,colors,sqsize,cols,rows=None):
""" top=object to drawin, sqsize=sqsize of the square
cols,rows = number of cols and rows
"""
if not rows: rows=cols ## can not put rows=cols in parameter list
cl=len(colors)
col=0
for j in range(0,rows*sqsize,sqsize):
for i in range (0,cols*sqsize,sqsize):
fill=colors[col]
top.create_rectangle(i,j,i+sqsize, j+sqsize,fill=fill)
col=(col+1)%cl
if not cols % cl: col=(col+1)%cl
if __name__=="__main__":
root = Tk()
root.title("Food Ordering System ")
numsq=8
sqsize=70
s=sqsize*numsq
middle=numsq/2*sqsize
root.geometry("%sx%s+%s+%s" % (s,s,0,0))
root.resizable(width=FALSE, height=FALSE)
currentframe = mainframe = create_main()
menuframe = create_menu()
registerframe = create_register()
helpframe = create_help()
helptext = create_food_ordering_system()
abouttext = create_about()
register_sucess_text = create_register_sucessful()
food_menu_text = create_food_menu()
go_main()
root.mainloop()
woooee 814 Nearly a Posting Maven
The short answer is to use withdraw or iconify but not all methods are available for all widgets. A simple example that creates a normal Tk window, and a second window with a canvas that is withdrawn when you click the "Continue" button, and is raised with the "deconify" button. This is a quickie and so is crude but illustrates this.
import Tkinter
class TestClass():
def __init__(self):
self.top = Tkinter.Tk()
self.top.title("Test")
self.top.geometry("200x150+500+5")
frame_1 = Tkinter.Toplevel()
frame_1.title("frame 1")
but_1 = Tkinter.Button(self.top, text='deiconify',
command=frame_1.deiconify)
but_1.pack(side="bottom", fill=Tkinter.X, expand=1)
but_2 = Tkinter.Button(self.top, text='Quit',
command=self.top.quit, bg='blue', fg='yellow')
but_2.pack(side="bottom", fill=Tkinter.X, expand=1)
can_1 = Tkinter.Canvas(frame_1)
can_1.pack()
can_1.create_rectangle(50,50,100,100, outline='white', fill='black')
cont = Tkinter.Button(frame_1, text='CONTINUE',
command=frame_1.withdraw, bg='red', fg='white' )
cont.pack(side="bottom", fill=Tkinter.X, expand=1)
self.top.mainloop()
##====================================================================
if __name__ == '__main__':
CT=TestClass()
Thropian 3 Junior Poster in Training
ok well thx for all the help guys I forgot about this awhile back when I found the canvas.pack_forget() command
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.