Reputation: 257
I have a Tkinter app that contains a menu. I can add cascades and commands in these cascades but can I add a command to the original app menu? The one that is created automatically on macOS and gets the name of the app:
Here is my code:
from tkinter import *
root = Tk()
menubar = Menu(root)
# I can create a cascade
cascade1 = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Cascade 1", menu=cascade1)
# And add a command to this cascade
cascade1.add_command(label="Command 1")
# How to add a command to the "Python" menu
# ???
root.config(menu=menubar)
root.mainloop()
Upvotes: 0
Views: 115
Reputation: 385970
I don't think it's possible to add a command anywhere on that menu except at the very top. However, you can add items at the top by creating a menu with the name "apple". Items you add to that menu will appear at the top of the application menu.
Note: you must configure this menu before assigning the menubar to the root window.
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
appmenu = tk.Menu(menubar, name='apple')
menubar.add_cascade(menu=appmenu)
appmenu.add_command(label='My Custom Command')
appmenu.add_separator()
root.configure(menu=menubar)
root.mainloop()
For reference, see Special menus in menubars in the the tcl/tk documentation.
Upvotes: 1