2015-11-23 34 views
0

我在網上發現了一些代碼來創建帶有多個框架的tkinter GUI。我試圖修改代碼,使其包含一個帶有導航按鈕的框架,以便在每一幀上顯示,而不是每次都重複該代碼。我不斷收到錯誤消息,無法確定如何將導航按鈕連接到相應的框架。 這是我得到的最接近代碼:在多個Tkinter GUI框架之間導航

import Tkinter as tk 


LARGE_FONT= ("Verdana", 12) 

class SeaofBTCapp(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, cont): 

     frame = self.frames[cont] 
     frame.tkraise() 


class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     label = tk.Label(self, text="Start Page", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button = tk.Button(self, text="Visit Page 1", 
         command=lambda: controller.show_frame(PageOne)) 
     button.pack() 

     button2 = tk.Button(self, text="Visit Page 2", 
         command=lambda: controller.show_frame(PageTwo)) 
     button2.pack() 


class PageOne(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Page One!!!", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button1 = tk.Button(self, text="Back to Home", 
         command=lambda: controller.show_frame(StartPage)) 
     button1.pack() 

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

     btns = MainButtonFrame(self,SeaofBTCapp) 
     btns.pack() 


class PageTwo(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button1 = tk.Button(self, text="Back to Home", 
         command=lambda: controller.show_frame(StartPage)) 
     button1.pack() 

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

     btns = MainButtonFrame(self,SeaofBTCapp) 
     btns.pack() 

class MainButtonFrame(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     Pages = [StartPage,PageOne,PageTwo ] 
     for button in Pages: 
      NewButton = tk.Button(self, text=str(button), 
         command=lambda: controller.show_frame(button)) 
      NewButton.pack() 

app = SeaofBTCapp() 
app.mainloop() 

,我得到了錯誤消息:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__ 
    return self.func(*args) 
    File "<ipython-input-13-dcc5fdbf1581>", line 103, in <lambda> 
    command=lambda: controller.show_frame(button)) 
TypeError: unbound method show_frame() must be called with SeaofBTCapp 
instance as first argument (got classobj instance instead) 

任何幫助將在暗示什麼,我做錯了在按鈕被大加讚賞命令。如果我能得到這個工作,我會用它作爲一個框架來構建一個更大的GUI。謝謝!

+0

你已經擁有了'button1'&'button2'不同幀之間進行導航....你爲什麼加入這些'btns = MainButtonFrame(個體經營,SeaofBTCapp)'? –

回答

0

MainButtonFrame的初始值設定項需要父級和控制器。控制器必須是主應用程序的實例,而不是。改變你如何創建MainButtonFrame是這樣的:

btns = MainButtonFrame(self,controller) 
+0

奇妙的是,這很好。謝謝 – JG23