2017-11-05 5 views
0

基本的に私が理解しようとしているのは、メニューバーの "ボタン"をページの変更にするにはどうすればいいですか?tkinterのメニューバーの方法でページを変更するには?

私はすでにドロップダウンメニューと各ページのクラスを作っています。私は、ページを変更するために別のボタンを作成するが、私はメニューバーを介してそれをやろうとしている場合、それは動作しますか?

私が見つけるすべてのチュートリアルでは、ボタンを介して行われ、そしてボタンのみ。..

私はtkinterで、Pythonの3.6を使用しています。

import tkinter as tk 


class MyApp(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 = {} 

     menu = tk.Menu(container) 

     betting = tk.Menu(menu, tearoff=0) 
     menu.add_cascade(menu=betting, label="Pages") 
     betting.add_command(label="PageOne", 
          command=lambda: controller.show_frame(PageOne)) 
     betting.add_command(label="PageTwo", 
          command=lambda: controller.show_frame(PageTwo)) 

     tk.Tk.config(self, menu=menu) 

     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="Home") 
     label.pack(pady=10, padx=10) 

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


# ***** PAGES ***** 

class PageOne(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Back to Home") 
     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() 


class PageTwo(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Page Two") 
     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() 


app = MyApp() 
app.geometry("1200x600") 
app.mainloop() 

今、私が知っている、私は今、「コントローラ&親を」解析されていないが、私が試したと試みた、そして何が私のために働かない...

+0

wxPythonやtkinterなどについてお尋ねしますか? –

+0

私の悪い、tkinter – ShrekTek

+1

で[mcve]を示してください。私たちは見えないコードを修正することはできません。 –

答えて

0

コントローラがMyAppです。 MyAppの定義内でcontrollerselfになります。したがって、controller.show_frameではなくself.show_frameに電話する必要があります。

betting.add_command(label="PageOne", 
        command=lambda: self.show_frame(PageOne)) 
betting.add_command(label="PageTwo", 
        command=lambda: self.show_frame(PageTwo)) 
+0

omg、私もこれを試していないと信じることができない、私はself.controller.show_frame )..など.. – ShrekTek

関連する問題