简体   繁体   中英

Why doesn't a part of this tkinter Menu widget not show up?

I am creating a Text editor and I am trying to edit the default Mac Application menu. Mac应用程序菜单 <—The Menu

I followed this tutorial to edit it and it works fine on its own like 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()

But when I try add in my menubar items for my text editor in another script, it will only display the text editor bits and not the edited application menu.

How can I get both to show at the same time?

Problem code:

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()

The example code from the tutorial does not work all the way for me (at least on my current OS 10.10). I wouldn't worry about changing the label manually if you are planning to make this a stand alone app. If you make this a stand alone app with py2app or cx_freeze, the name of you app you set in your setup.py script will show in the menubar as YourAppName ; and the same goes for the about YourAppName .

There is a partial solution; reorder your code. If you move the section that creates the About this app object just after the menu is created, it will work. Here is how it would look in your code:

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

Unfortunately, this does not change the name of the main menubar item. But if you make this an app as I decried above, you don't need to worry about this.

EDIT

I found a potential solution that may help you change the the default Python menu item to whatever you want. Go to this post I found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM