gpt4 book ai didi

Python GUI 菜单加载另一个框架

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:23 25 4
gpt4 key购买 nike

开发者们好,如何改变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 GUI 菜单加载另一个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45763471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com