从菜单打开新框架(已打开窗口中的新窗口)

时间:2017-12-22 19:53:21

标签: python python-3.x tkinter

我正在使用一部分代码在一个窗口中打开不同的页面,它工作正常。当我尝试从菜单(文件> New Test)打开一个窗口时,它会打开一个新窗口而不是同一个窗口。我对此感到困惑并且有几个问题。

  1. 如何在同一窗口中从菜单中打开窗口。

  2. 为什么菜单页面在Page1(NewTestWindow)中可见,如果它在MainPage中定义。

  3. 是否为菜单栏创建单独的类是正确的方法,或者最好将其插入到MainWindow类中。

  4.     import tkinter as tk
        from tkinter import font as tkfont

    class MenuBar(tk.Menu): def __init__(self, parent, controller): tk.Menu.__init__(self, controller) self.controller = controller fileMenu = tk.Menu(self, tearoff=0) self.add_cascade(label="File", underline=0, menu=fileMenu) fileMenu.add_command(label="New Test", underline=1, command=lambda: SampleApp().show_frame("NewTestWindow")) fileMenu.add_separator() fileMenu.add_command(label="Exit", underline=2, command=self.onexit) def onexit(self): quit() class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic") 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 (MainWindow, NewTestWindow): page_name = F.__name__ frame = F(parent=container, controller=self) self.frames[page_name] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame("MainWindow") def show_frame(self, page_name): '''Show a frame for the given page name''' frame = self.frames[page_name] frame.tkraise() class MainWindow(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller self.menubar = MenuBar(self, parent) self.controller.config(menu=self.menubar) label = tk.Label(self, text="This is the main page", font=controller.title_font) label.pack(side="top", fill="x", pady=10) button1 = tk.Button(self, text="Start new test", command=lambda: controller.show_frame("NewTestWindow")) button1.pack() class NewTestWindow(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller label = tk.Label(self, text="This is page 1", font=controller.title_font) label.grid(row=1, column=0) button = tk.Button(self, text="Go to the start page", command=lambda: controller.show_frame("MainWindow")) button.grid(row=2, column=0) if __name__ == "__main__": app = SampleApp() app.mainloop()

1 个答案:

答案 0 :(得分:0)

更改此内容:

command=lambda: SampleApp().show_frame("NewTestWindow")

对此:

command=lambda: self.show_frame(NewTestWindow)