2017-11-05 12 views
0

기본적으로 내가 알아 내려고하는 것은, 내 메뉴 막대 "버튼"을 페이지를 어떻게 변경합니까?tkinter의 내 메뉴 막대 방법을 통해 페이지를 변경하는 방법?

이미 드롭 다운 메뉴와 각 페이지의 수업을 만들었습니다. 문제는 내가 페이지를 바꿀 별도의 버튼을 만들면 작동하지만, 메뉴 바를 통해 할 때가 아닐까요?

내가 찾을 수있는 모든 튜토리얼은 버튼을 통해, 그리고 단추 만 ..

나는 tkinter으로, 파이썬 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