hithwen
hithwen

Reputation: 2193

Tkinter edit menu, special characters options in macos

I'm creating a little tkinter app. It has a text field and now I'm adding a menu as follows:

def _make_top_bar(self):
    menubar = Menu(self.parent)
    filemenu = # File menu definition, not relevant
    menubar.add_cascade(label="File", menu=filemenu)

    editmenu = Menu(menubar, tearoff=0)
    editmenu.add_command(label="Clear", command=self.clear) # It clears the text field
    menubar.add_cascade(label="Edit", menu=editmenu)
    root.config(menu=menubar)

This adds a menu with the File menu as I defined but the Edit menu has two extra options: Start dictation and Special Characters (I have not defined them) . Start dictations opens voice recognition. Special Characters opens an emoji selector.

edit menu

emoji selector

I don't really want an emoji selector in my app specially because it causes an error. If I double click on an emoji I get a long stack trace:

2014-04-01 13:09:28.283 Python[4557:d07] -[NSConcreteMutableAttributedString characterAtIndex:]: unrecognized selector sent to instance 0x7fd83bf55ee0
2014-04-01 13:09:28.286 Python[4557:d07] (
0   CoreFoundation                      0x00007fff8fd8925c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff9194ce75 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8fd8c12d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00007fff8fce73f2 ___forwarding___ + 1010
4   CoreFoundation                      0x00007fff8fce6f78 _CF_forwarding_prep_0 + 120
5   libtk8.6.dylib                      0x0000000108fe3b54 -[TKContentView(TKKeyEvent) insertText:] + 244
6   AppKit                              0x00007fff8d1fe767 -[NSTextInputContext insertText:replacementRange:] + 379
7   AppKit                              0x00007fff8d1fda18 -[NSTextInputContext handleTSMEvent:] + 8271
8   AppKit                              0x00007fff8d1fb9a5 _NSTSMEventHandler + 205
9   HIToolbox                           0x00007fff92fd01d4 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 892
10  HIToolbox                           0x00007fff92fcf787 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 385
11  HIToolbox                           0x00007fff92fe3880 SendEventToEventTarget + 40
...

How can I get rid of the "Special characters" entry in my menu?

Upvotes: 1

Views: 495

Answers (1)

R.Jarvis
R.Jarvis

Reputation: 273

I've not found a way to remove the items from the menu, but there is a way to not have them appear in a menu in the first place.

Instead of calling the menu "Edit", call it "Edit " (with a space at the end).

This stops MacOS from adding the extra items, and doesn't look any different to the user:

def _make_top_bar(self):
    menubar = Menu(self.parent)
    filemenu = # File menu definition, not relevant
    menubar.add_cascade(label="File", menu=filemenu)

    editmenu = Menu(menubar, tearoff=0)
    editmenu.add_command(label="Clear", command=self.clear) # It clears the text field
    menubar.add_cascade(label="Edit ", menu=editmenu)
    root.config(menu=menubar)

Upvotes: 1

Related Questions