How to get the "Mac" command character in the Tkinter menu

I wrote a Python program for business for friends and use Tkinter for the interface. So far, all functions have been added to the main program window, but now I add a print function, create a simple "File" menu and want to add a "Print" entry to this menu, including displaying the corresponding key combination.

On my Mac, I want the shortcut to be Command-P. I found the meaning of the "Unicode" Mac command character and tried various ways to create an accelerator string that simply combines this character and the letter "P", but nothing works. I get either a character or a letter to display in the menu next to "Print", but never both.

Here is a complete line of code that adds a menu item, with the last attempt to build a line (I believe I found this unicode.join parameter unicode.join in the stack overflow):

 sub_menu.add_command(label="Print", command=self.print_, accelerator=unicode.join(u"\u2318", u"P")) // Only the "P" displays 

Here are some of the other options I've tried (lines for shortening lines). With each of these options, only the "command" symbol appears:

 accelerator=u"\u2318\u0050" accelerator=u"\u2318" + "P" accelerator=u"\u2318" + u"P" accelerator=u"\u2318P" accelerator=u"".join([u"\u2318", u"P"]) 

So far, I have not had much to understand about Unicode strings, so maybe I am doing something wrong in this regard. However, all the attempts that I made came as a result of various searches, both here and elsewhere, and so far nothing has worked. Any understanding of how to make this work would be very welcome!

Python 2.7.3, Mac OS X 10.8.3

+4
source share
2 answers

After further searching the Internet, I finally found a page that has a solution. I was surprised (and annoyingly touched) that this solution is different from that described in the PDF documentation for Tkinter 8.4, which I mentioned so far (published in New Mexico Tech). I found links to the Tkinter 8.5 documentation, but they also list the wrong process.

In any case, it is much simpler than I thought, and similar to the syntax used to bind keys, but a little different. Instead of directly including the command character in the accelerator line, Tkinter takes the literal word β€œCommand” (or the abbreviated β€œCmd”) and internally converts it to the displayed β€œβŒ˜β€ 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:

 Print ⌘P 

As you can see from the page above, similar arrow words exist for other modifier keys, and on Mac OS X they all automatically translate into their graphic equivalents.

+1
source

It was really very simple, all I did was use string formatting to combine text="%s%s" % (u"\u2318","P")

Here is an example of the Tkinter App , the display is small, but it shows what you need.

 import Tkinter as tk class 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 
+1
source

Source: https://habr.com/ru/post/1483690/


All Articles