Home > front end >  Add commands to the "Python" menu with Tkinter on macOS - Python 3
Add commands to the "Python" menu with Tkinter on macOS - Python 3

Time:01-31

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:

enter image description here

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

CodePudding user response:

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

screenshot of menu

For reference, see Special menus in menubars in the the tcl/tk documentation.

  •  Tags:  
  • Related