哈罗开发者
如何更改onclick框架页面菜单的级联?
注释掉:#,command = lambda:controller.show_frame(Interface))

import tkinter as tk
import config.window_config as conf
from pages.interface import Interface
from pages.settings import Settings


class network_tools(tk.Tk):

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

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

        self.title(conf.title)
        self.geometry(conf.geometry)

        # menu
        menubar = tk.Menu(self)

        # menu pattern
        #** here is the problem **
        interface = tk.Menu(menubar, tearoff=0)#, command=lambda: controller.show_frame(Interface))
        settings = tk.Menu(menubar, tearoff=0)#, command=lambda: controller.show_frame(Settings))

        menubar.add_cascade(label="Interface", menu=interface)
        menubar.add_cascade(label="Settings", menu=settings)
        self.config(menu=menubar)

        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        #** frames loading **
        self.frames = {}

        for F in (Interface, Settings):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, stick="nsew")

        # default upstart frame
        self.show_frame(Interface)

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

app = network_tools()
app.mainloop()

最佳答案

级联菜单模式应如下所示:

本示例定义了2个菜单来访问一个菜单中的Interface和另一个菜单中的Settings

menubar = tk.Menu(self)

interface = tk.Menu(menubar, tearoff=0)
settings = tk.Menu(menubar, tearoff=0)

interface.add_command(label="Interface", command=lambda: self.show_frame(Interface))
menubar.add_cascade(label='Interface', menu=interface)

settings.add_command(label="Settings", command=lambda: self.show_frame(Settings))
menubar.add_cascade(label='Settings', menu=settings)
self.config(menu=menubar)




此示例仅使用1个菜单收集2个帧:

menubar = tk.Menu(self)

# menu pattern

screens = tk.Menu(menubar, tearoff=0)

screens.add_command(label="Interface", command=lambda: self.show_frame(Interface))
screens.add_command(label="Settings", command=lambda: self.show_frame(Settings))
menubar.add_cascade(label='Screens', menu=screens)

self.config(menu=menubar)

关于python - Python GUI菜单加载另一帧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45763471/

10-16 22:16