Tkinter 在 Frames 中添加菜单栏 [英] Tkinter add menu bar in Frames

查看:83
本文介绍了Tkinter 在 Frames 中添加菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处使用了答案中发布的代码 在 tkinter 中的两帧之间切换来自@Bryan Oakley.现在我想添加一些菜单栏.在 Pageone 中我想添加菜单 FileHelp,在 Pagetwo 中我想添加菜单 Dataplot但我做不到.

I was using the code posted in answer here Switch between two frames in tkinter by @Bryan Oakley. Now I want to add some menu bar. In Pageone I want to add menus File and Help and in Pagetwo I want to add menus Data and plot But I cant get it done.

我可以向主窗口添加菜单.但是随后它可用于所有页面,并且在我更改页面时不会消失.我不想要那个.我想添加特定于该页面的菜单栏.我该怎么做

I can add menus to to the main window. But then it is available to all the pages and doesn't go away when I change the page. I don't want that. I want to add menubar specific to that page. How can I do that

编辑

实际上我想做的是,我正在制作一个 GUI,您可以在其中进行数学积分和矩阵计算等.因此,在打开 GUI 时,会出现带有积分、矩阵等按钮的窗口.单击这些按钮将打开一个窗口,您可以在其中进行相应的计算.我是使用 Toplevel() 命令完成的,它会打开一个新窗口,而我只是隐藏了前一个窗口.但我喜欢 Bryan 在该链接中发布的 GUI 代码,因此我询问了向单个页面添加菜单的问题.

Actually what i want to do that is that, I am making a GUI where you can do mathematical integration and matrix calculation and so on. So on opening the GUI there will be window with buttons for Integration, matrix etc. And clicking on those will open a window where you can do corresponding calculation. I did it using the Toplevel() command which opens up a new window and I just hide the previous. But I liked GUI code posted by Bryan in that link, so I asked about adding menu to individual pages.

在 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.

推荐答案

最简单的解决方案是向每个页面添加一个方法,为该页面创建菜单栏,然后​​在切换框架时切换到该菜单.

The simplest solution is to add a method to every page that creates the menubar for that page, and then switch to that menu when you switch frames.

这可能需要您为页面之间通用的所有菜单项(例如:编辑->剪切、编辑-复制、编辑-粘贴等)准备一堆重复代码,但至少展示了一般技术.

This will likely require you to have a bunch of duplicate code for all of the menu items that will be common between pages (eg: edit->cut, edit-copy, edit-paste, etc), but it at least shows the general technique.

例如,一个页面可能会定义这样的方法:

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 中添加菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆