2015-03-25 65 views
0

我是新来的Tkinter,我正在努力与什么应该是我的RasPi相当简单的一点python代码。我的目标是制作一系列帧,它们都具有相同的固定尺寸和背景图像,但具有不同的按钮,标签(位于图像顶部)和功能。如果我在容器中包含一个画布和一个图像,那么我可以得到我的框架(至少是第一帧)以显示背景图像,但是我无法向页面特定类中的画布添加任何内容,因为我无法再次参考画布?如果我将框架留在容器中,然后在页面特定的类中添加画布/图像,则无法使画布/图像正常工作。这是我想要修改以满足我的需要的代码...Python Tkinter多帧与背景图像

import Tkinter as tk 

TITLE_FONT = ("Helvetica", 18, "bold") 
class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

    container = tk.Frame(self) 
    container.pack(side="top", fill="both", expand=True) 
    container.grid_rowconfigure(0, weight=1) 
    container.grid_columnconfigure(0, weight=1) 

    self.frames = {} 
    for F in (StartPage, PageOne, PageTwo): 
     frame = F(container, self) 
     self.frames[F] = frame 
     frame.grid(row=0, column=0, sticky="nsew") 

    self.show_frame(StartPage) 

    def show_frame(self, c): 
     frame = self.frames[c] 
     frame.tkraise() 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="This is the start page",     font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 

     button1 = tk.Button(self, text="Go to Page One", 
         command=lambda: controller.show_frame(PageOne)) 
     button2 = tk.Button(self, text="Go to Page Two", 
         command=lambda: controller.show_frame(PageTwo)) 
     button1.pack() 
     button2.pack() 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="This is page 1", font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 
     button = tk.Button(self, text="Go to the start page", 
         command=lambda: controller.show_frame(StartPage)) 
     button.pack() 

class PageTwo(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="This is page 2", font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 
     button = tk.Button(self, text="Go to the start page", 
         command=lambda: controller.show_frame(StartPage)) 
     button.pack() 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop()controller.show_frame(PageTwo)) 

回答

0

我得到它的工作...

import Tkinter as tk 
import ttk 

TITLE_FONT = ("Helvetica", 18, "bold") 
class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 
     self.attributes("-fullscreen", True) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 
     for F in (StartPage, PageOne, PageTwo): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, c): 
     frame = self.frames[c] 
     frame.tkraise() 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="/home/pi/Saffi.gif") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is the start page", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 

     button1 = tk.Button(self, text="Go to Page One", 
         command=lambda: controller.show_frame(PageOne)) 
     button2 = tk.Button(self, text="Go to Page two", 
         command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button2.place(x=300,y=406,width=200,height=44) 
     button3.place(x=500,y=406,width=80,height=44) 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="/home/pi/Saffi.gif") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is page one", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 

     button1 = tk.Button(self, text="Go to Start Page", 
         command=lambda: controller.show_frame(StartPage)) 
    #button2 = tk.Button(self, text="Go to Page two", 
    #     command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button3.place(x=300,y=406,width=200,height=44) 

class PageTwo(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     logo = tk.PhotoImage(file="/home/pi/Saffi.gif") 
     BGlabel = tk.Label(self,image=logo) 
     BGlabel.image = logo 
     BGlabel.place(x=0,y=0,width=592,height=450) 
     label = tk.Label(self, text="This is page two", font=TITLE_FONT) 
     label.place(x=0,y=0,width=592,height=44) 

     button1 = tk.Button(self, text="Go to Start Page", 
         command=lambda: controller.show_frame(StartPage)) 
    #button2 = tk.Button(self, text="Go to Page two", 
    #     command=lambda: controller.show_frame(PageTwo)) 
     button3 = tk.Button(self, text="Exit", 
         command=self.quit) 
     button1.place(x=100,y=406,width=200,height=44) 
     button3.place(x=300,y=406,width=200,height=44) 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop()