使用类

时间:2017-03-13 16:30:22

标签: python tkinter menu

我已经搜索过这个但是根据我发现的内容得不到完整答案。 我正在使用tkinter在Python3中编写GUI。一切都将在课堂上,包括一个菜单'类。 情侣问题 - 我想使用for循环来填充我的菜单项和子项(尚未实现)。   - 我想为“容器”的每个框架执行此操作。类显示但我希望在这些选项不适用时禁用(灰显)菜单中的某些项目。即,在没有任何内容加载的情况下启动程序'保存'按钮没有意义所以我会禁用它直到一个新的'制作项目或打开现有项目。

我的代码到目前为止。

import tkinter as tk
from tkinter import ttk, Menu


class MainFrame(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)


        #display manubar
        menubar = MenuBar(self)
        self.config(menu=menubar)

        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)

class PageOne(tk.Frame):

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

        button = ttk.Button(self, text="Visit Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        button1 = ttk.Button(self, text="Go to Start Page",
                             command=lambda: controller.show_frame(StartPage))

        button.pack()
        button1.pack()


class PageTwo(tk.Frame):

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

        button = ttk.Button(self, text="Visit Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button1 = ttk.Button(self, text="Go to Start Page",
                             command=lambda: controller.show_frame(StartPage))

        button.pack()
        button1.pack()


class MenuBar(tk.Menu):
    def __init__(self, parent):
        tk.Menu.__init__(self, parent)

        fileMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label="File",underline=0, menu=fileMenu)
        fileMenu.add_command(label="Page One", underline=0,
                             command=self.onexit)
        fileMenu.add_command(label="Page Two", underline=1,
                             command=lambda:self.onexit)
        fileMenu.add_command(label="Exit", underline=1, command=self.onexit)


    def onexit(self):
        quit()

if __name__ == "__main__":
    app = MainFrame()
    app.mainloop()

你如何称呼' PageOne'或者' Pagetwo'使用文件菜单中的选项?出于测试目的,我现在只是退出程序,所以我可以毫无错误地运行程序。

1 个答案:

答案 0 :(得分:1)

  

...我不知道如何使用文件菜单中的选项调用'PageOne'或'Pagetwo'...

就像您的页面一样,MenuBar对象需要被赋予对控制器的引用,以便它可以调用控制器函数show_frame。由于MainFrame既是“父”又是“控制器”,因此可以将这两个参数用于这两个目的。

class MenuBar(tk.Menu):
    def __init__(self, parent):
        self.controller = parent
        ...
        fileMenu.add_command(..., command=lambda: self.controller.show_frame(PageOne))
        fileMenu.add_command(..., command=lambda: self.controller.show_frame(PageTwo))
        ...