2015-03-25 63 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()