classification
Title: tkinter throws exception when key is pressed
Type: behavior Stage: resolved
Components: macOS, Tkinter Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: International keyboard makes IDLE crash on OSX
View: 22566
Assigned To: Nosy List: epaine, ned.deily, ronaldoussoren, serhiy.storchaka, spacether
Priority: normal Keywords:

Created on 2020-12-14 18:22 by spacether, last changed 2020-12-20 19:44 by spacether. This issue is now closed.

Messages (9)
msg382998 - (view) Author: Justin (spacether) Date: 2020-12-14 18:22
On macOs 10.14.6 laptop with a qwerty layout, when I switch my keyboard layout to `French - PC` and use a tkinter keyboard listener
and press the '[' button which should be the '^' button on the azerty keyboard, this Error is thrown:
```
2020-12-14 10:13:26.533 Python[14880:1492926] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff41ce49ad __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff6c3dfa17 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff41ce47df +[NSException raise:format:] + 201
	3   CoreFoundation                      0x00007fff41c5a159 -[__NSCFString characterAtIndex:] + 102
	4   Tk                                  0x00007fff4da8a806 TkpInitKeymapInfo + 719
	5   Tk                                  0x00007fff4da905e9 Tk_MacOSXSetupTkNotifier + 793
	6   Tcl                                 0x00007fff4d98c48e Tcl_DoOneEvent + 301
	7   _tkinter.cpython-38-darwin.so       0x00000001090802de _tkinter_tkapp_mainloop + 342
	8   Python                              0x0000000108ac1b0a method_vectorcall_FASTCALL + 250
	9   Python                              0x0000000108b5a299 call_function + 346
	10  Python                              0x0000000108b51457 _PyEval_EvalFrameDefault + 3895
	11  Python                              0x0000000108b5ae5d _PyEval_EvalCodeWithName + 2107
	12  Python                              0x0000000108abad39 _PyFunction_Vectorcall + 217
	13  Python                              0x0000000108abcc7d method_vectorcall + 135
	14  Python                              0x0000000108b5a299 call_function + 346
	15  Python                              0x0000000108b51477 _PyEval_EvalFrameDefault + 3927
	16  Python                              0x0000000108b5ae5d _PyEval_EvalCodeWithName + 2107
	17  Python                              0x0000000108b5047d PyEval_EvalCode + 51
	18  Python                              0x0000000108b89025 run_eval_code_obj + 102
	19  Python                              0x0000000108b88473 run_mod + 82
	20  Python                              0x0000000108b87345 PyRun_FileExFlags + 160
	21  Python                              0x0000000108b86a29 PyRun_SimpleFileExFlags + 271
	22  Python                              0x0000000108b9e449 Py_RunMain + 1870
	23  Python                              0x0000000108b9e790 pymain_main + 306
	24  Python                              0x0000000108b9e7de Py_BytesMain + 42
	25  libdyld.dylib                       0x00007fff6dbae3d5 start + 1
	26  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
```
One can verify this by running this program on a macOs laptop with a qwerty keyboard, switching the layout to French - PC and pressing the '[' key.
```
import pygame
pygame.init()
pygame.display.set_mode((100, 100))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            key_name = pygame.key.name(event.key)
            print(event, event.key.__class__, event.key, key_name)
        elif event.type == pygame.KEYUP:
            key_name = pygame.key.name(event.key)
            print(event, event.key.__class__, event.key, key_name)
```
```
msg382999 - (view) Author: Justin (spacether) Date: 2020-12-14 18:24
Sorry my verification keyboard listener program is not the pygame one. It is this one:
```
from tkinter import *
def keyup(e):
    print('up', e.__dict__)
def keydown(e):
    print('down', e.__dict__)

root = Tk()
frame = Frame(root, width=100, height=100)
frame.bind("<KeyPress>", keydown)
frame.bind("<KeyRelease>", keyup)
frame.pack()
frame.focus_set()
root.mainloop()
```
msg383004 - (view) Author: E. Paine (epaine) * Date: 2020-12-14 21:06
Thank you for reporting this. I just have a few questions:
1. Which Python version are you using?
2. If Python 3.9.1, which installer did you use?
3. What does `tkinter.test.support.get_tk_patchlevel()` output?
4. Does your pygame example suffer the same issue (I assume not)?

I am unable to reproduce on macOS 11 with Python 3.8.6, but am almost certain this is a Tk issue due to the nature of it.
msg383006 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-12-14 21:41
The problem here undoubtedly is that the Python you are using is linked with the Apple-provided system version of Tk 8.5.9 which exhibits this nasty behavior of crashing when pressing a composing key. The system version of Tk is woefully out of date with other serious bugs and hasn’t been updated by Apple for many macOS releases despite many bug reports. Their response has been to deprecate the system Tk and they will eventually remove it. That’s why Python binary installers from Python.org now provide their own copy of Tk 8.6. You should update to a macOS build of Python 3.8 or 3.9 that also uses a newer version of Tk. Issue22566 has more details.
msg383019 - (view) Author: Justin (spacether) Date: 2020-12-14 23:29
Thanks for responding so quickly.
1. My python version is:
Python 3.8.6 (default, Oct  8 2020, 14:07:53) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
2. N/A
3. >>> from tkinter.test.support import *
>>> get_tk_patchlevel()
(8, 5, 9, 'final', 0)
4. No; the pygame sample does not crash with this key input

Which version of TK 8.5.X fixes this, 8.5.18.0? That's what it looks like here: https://www.python.org/download/mac/tcltk/
msg383026 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-12-15 01:16
> Which version of TK 8.5.X fixes this, 8.5.18.0?

Yes, that version of Tk 8.5 does not have this problem. But Tk 8.5 is no longer supported by the Tcl project; we recommend the use of Tk 8.6. In general, you usually cannot just swap Tk versions; the Python instance you are using needs to be built with the right versions of Tcl and Tk. You should check with the provider of the Python you are using and get a more current Python that uses a current version of Tcl and Tk.
msg383438 - (view) Author: Justin (spacether) Date: 2020-12-20 18:35
FYI, I just brew installed python and with:
```
Python 3.9.1 (default, Dec 17 2020, 03:56:09) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
```
This issue still happens
msg383440 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-12-20 18:56
As explained above, this crash will happen with any Python that links to the deprecated Apple-supplied system Tk framework in macOS versions from macOS 10.7 to 11 Big Sur. If you want to use tkinter, you need to use a Python that links to a newer version of Tk. If Homebrew’s Python is not doing that, you should bring it up with the Homebrew project.
msg383443 - (view) Author: Justin (spacether) Date: 2020-12-20 19:44
Thank you very much. I understand and just wanted to let you know.
In brew I opened up this ticket(https://github.com/Homebrew/homebrew-core/issues/67327) with that team.
History
Date User Action Args
2020-12-20 19:44:38spacethersetmessages: + msg383443
2020-12-20 18:56:37ned.deilysetmessages: + msg383440
2020-12-20 18:35:21spacethersetmessages: + msg383438
2020-12-15 01:16:10ned.deilysetmessages: + msg383026
2020-12-14 23:29:03spacethersetmessages: + msg383019
2020-12-14 21:41:03ned.deilysetstatus: open -> closed
superseder: International keyboard makes IDLE crash on OSX
messages: + msg383006

resolution: duplicate
stage: resolved
2020-12-14 21:06:21epainesetversions: + Python 3.8, Python 3.9, Python 3.10
nosy: + ronaldoussoren, ned.deily, serhiy.storchaka, epaine

messages: + msg383004

components: + macOS, Tkinter
type: behavior
2020-12-14 18:24:18spacethersetmessages: + msg382999
2020-12-14 18:22:37spacethercreate