Python GUI菜单加载另一个框架

时间:2017-08-18 19:02:00

标签: python tkinter

你好开发者, 如何更改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()

1 个答案:

答案 0 :(得分:1)

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

此示例定义了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)