Tkinter - 如何在另一个类中修改类中的窗口小部件?

时间:2018-05-26 18:51:28

标签: python class tkinter tkinter-menu

我希望能够在LoginPage期间将菜单更改为无效,但在NextPage之后变为有效,是否有任何方法可以在Application的外侧执行此操作类?

import tkinter as tk
from tkinter import ttk


class Application(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        window = tk.Frame(self)
        window.pack(side="top", fill="y", expand=True)

        self.menu = tk.Menu(window)
        file_menu = tk.Menu(self.menu, tearoff=0)
        file_menu.add_command(label="Temp")
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=quit)
        self.menu.add_cascade(label="File", menu=file_menu)
        tk.Tk.config(self, menu=self.menu)

        # Disable the menu for LoginPage
        self.disable_menu("File")

        self.frames = {}
        for frames in (LoginPage, NextPage):
            frame = frames(window, self)
            self.frames[frames] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LoginPage)

    def enable_menu(self, sub_menu):
        self.menu.entryconfig(sub_menu, state="normal")

    def disable_menu(self, sub_menu):
        # Can this function be called from the class NextPage to disable the menu when not logged in?
        self.menu.entryconfig(sub_menu, state="disabled")

    def show_frame(self, controller):
        frame = self.frames[controller]
        frame.tkraise()


class LoginPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        button = ttk.Button(self, text="Next Page", command=lambda: controller.show_frame(NextPage))
        button.pack()


class NextPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        # Enable menu bar here after logged in
        label = ttk.Label(self, text="Temporary")
        label.pack()


application = Application()
application.mainloop()

我试过了: 课程Application.disable_menu(self, sub_menu="File")中的NextPage,但它说'NextPage' object has no attribute 'menu',无论如何都可以使这项工作成功吗?

0 个答案:

没有答案