本文介绍了Tkinter 在 Frames 中添加菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此处使用了答案中发布的代码 在 tkinter 中的两帧之间切换来自@Bryan Oakley.现在我想添加一些菜单栏.在 Pageone 中我想添加菜单 File
和 Help
,在 Pagetwo 中我想添加菜单 Data
和 plot
但我做不到.
我可以向主窗口添加菜单.但是随后它可用于所有页面,并且在我更改页面时不会消失.我不想要那个.我想添加特定于该页面的菜单栏.我该怎么做
在 Bryan 的代码中,我刚刚添加了这些行`class SampleApp(tk.Tk):
In the Bryan's code I just added these lines`class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
menu=tk.Menu(self)
self.config(menu=menu)
subMenu=tk.Menu(menu)
menu.add_cascade(label="File",menu=subMenu)`
但正如我所说,它将菜单附加到主窗口.但我想要不同页面的不同菜单.
But as I said it attach the menu to the main window. But I want different menu for different pages.
推荐答案
最简单的解决方案是向每个页面添加一个方法,为该页面创建菜单栏,然后在切换框架时切换到该菜单.
这可能需要您为页面之间通用的所有菜单项(例如:编辑->剪切、编辑-复制、编辑-粘贴等)准备一堆重复代码,但至少展示了一般技术.
For example, a page might define a method like this:
class PageOne(tk.Frame):
...
def menubar(self, root):
menubar = tk.Menu(root)
pageMenu = tk.Menu(menubar)
pageMenu.add_command(label="PageOne")
menubar.add_cascade(label="PageOne", menu=pageMenu)
return menubar
You would then call this method when you switch frames, like this:
def SampeApp(tk.Tk):
...
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
menubar = frame.menubar(self)
self.configure(menu=menubar)
这篇关于Tkinter 在 Frames 中添加菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!