How do I get the Mac "command" symbol in a Tkinter menu How do I get the Mac "command" symbol in a Tkinter menu tkinter tkinter

How do I get the Mac "command" symbol in a Tkinter menu


After further searching online, I finally found a page that has the solution. I was surprised (and a touch annoyed) that this solution was different than the one outlined in the PDF documentation for Tkinter 8.4 that I've been referencing thus far (the one published by New Mexico Tech). I found links to Tkinter 8.5 documentation, but they also list the incorrect process.

Anyway, it's a lot simpler than I thought, and is similar to the syntax used for key binding, but slightly different. Instead of directly including the command symbol in the accelerator string, Tkinter takes the literal word "Command" (or the abbreviated "Cmd"), and internally converts it to the displayable character "⌘" in the menu. So my resulting line is:

sub_menu.add_command(label="Print", command=self.print_, accelerator="Command-P")

...and what I get in my full menu item is:

Print ⌘P

As the page linked above shows, similar shortcut words exist for other modifier keys, and on Mac OS X, these are all automatically translated into their graphical equivalents.


It was actually really easy, all I did was use string formatting to concatenate text="%s%s" % (u"\u2318","P")

Here is a sample Tkinter App, the display is small but it shows what you need.

import Tkinter as tkclass SampleApp(tk.Tk):    def __init__(self, *args, **kwargs):        tk.Tk.__init__(self, *args, **kwargs)        self.label = tk.Label(text="%s%s" % (u"\u2318","P"))        self.label.pack(padx=10, pady=10)app = SampleApp()app.mainloop()

Output:

⌘P