Tkinter - Как изменить виджет в классе из другого класса?
Я хочу изменить меню, чтобы оно было неактивным во время 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'
Есть ли способ заставить это работать?