【问题标题】:Why doesn't a part of this tkinter Menu widget not show up?为什么这个 tkinter 菜单小部件的一部分不显示?
【发布时间】:2016-01-23 17:44:31
【问题描述】:

我正在创建一个文本编辑器,我正在尝试编辑默认的 Mac 应用程序菜单。

我按照this 教程对其进行了编辑,它自己可以正常工作,如下所示:

from Tkinter import *
root=Tk()
menubar = Menu(root)
appmenu = Menu(menubar, name='apple')
menubar.add_cascade(menu=appmenu)
appmenu.add_command(label='About My Application')
appmenu.add_separator()
root['menu'] = menubar
root.mainloop()

但是当我尝试在另一个脚本中为我的文本编辑器添加菜单栏项时,它只会显示文本编辑器位,而不是已编辑的应用程序菜单。

如何让两者同时显示?

问题代码:

from Tkinter import *
class Main(object):
    def __init__(self, root):
        root.title("PyText")
        #Text editor menu items
        self.m1=Menu(root)

        self.fm=Menu(self.m1, tearoff=0)
        self.fm.add_command(label="New", command=self.saveas)
        self.fm.add_command(label="Open", command=self.saveas)
        self.fm.add_command(label="Save", command=self.saveas)
        self.fm.add_command(label="Save as...", command=self.saveas)
        self.fm.add_command(label="Close", command=self.saveas)
        self.fm.add_separator()
        self.fm.add_command(label="Exit", command=root.quit)
        self.m1.add_cascade(label="File", menu=self.fm)

        self.editmenu = Menu(self.m1, tearoff=0)
        self.editmenu.add_command(label="Undo", command=self.saveas)
        self.editmenu.add_separator()
        self.editmenu.add_command(label="Cut", command=self.saveas)
        self.editmenu.add_command(label="Copy", command=self.saveas)
        self.editmenu.add_command(label="Paste", command=self.saveas)
        self.editmenu.add_command(label="Delete", command=self.saveas)
        self.editmenu.add_command(label="Select All", command=self.saveas)
        self.editmenu.add_separator()
        self.m1.add_cascade(label="Edit", menu=self.editmenu)
        #Problem code here
        self.appmenu = Menu(self.m1, name="apple", tearoff=0)
        self.m1.add_cascade(menu=self.appmenu)
        self.appmenu.add_command(label="About PyText")
        self.appmenu.add_separator()

        root.config(menu=self.m1)



        self.t1=Text(root)
        self.t1.config(width=90, height=40)
        self.t1.grid()

    def saveas(self):
        self.filewin = Toplevel()
        self.filewin.title("Name")
        self.e1=Entry(self.filewin)
        self.e1.grid()
        self.button = Button(self.filewin, text="Save", command=self.save)
        self.button.grid()

    def save(self):
        with open(self.e1.get(), "w") as f: # this instance variable can be accessed
            f.write(self.t1.get('1.0', 'end'))


root = Tk()
app = Main(root)
root.mainloop()

【问题讨论】:

  • 教程中的内容对我来说不起作用,而不是显示apple 我得到()。只需更改它以匹配您的其他菜单代码。 self.m1.add_cascade(menu=self.appmenu, label="apple")

标签: python macos menu tkinter


【解决方案1】:

本教程中的示例代码对我来说并不适用(至少在我当前的 OS 10.10 上)。如果您打算将其作为一个独立的应用程序,我不会担心手动更改标签。如果您使用 py2app 或 cx_freeze 将其设为独立应用程序,您在 setup.py 脚本中设置的应用程序名称将在菜单栏中显示为 YourAppNameabout YourAppName 也是如此。

有部分解决方案;重新排序您的代码。如果在创建菜单后移动创建About this app 对象的部分,它将起作用。以下是它在您的代码中的外观:

class Main(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title("PyText")
        #Text editor menu items
        self.m1=Menu(self)

        self.appmenu = Menu(self.m1, name="apple")
        self.m1.add_cascade(menu=self.appmenu)
        self.appmenu.add_command(label="About PyText")
        self.appmenu.add_separator()

        # Other menu bar code...

不幸的是,这不会更改主菜单栏项的名称。但是,如果您按照我上面的谴责将其作为应用程序,则无需担心。

编辑

我找到了一个潜在的解决方案,可以帮助您将默认的 Python 菜单项更改为您想要的任何内容。转到this post我找到了。

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多