简体   繁体   English

tkinter 在 ipython 上崩溃,但在 python 中没有(在 M1 Mac 上)

[英]tkinter crashes on ipython, but not in python (on a M1 Mac)

I am using a miniconda installation on an M1 Mac and tkinter crashes when I call it from ipython , but not from python`.我在 M1 Mac 上使用 miniconda 安装, tkinter crashes when I call it from , but not from python` 调用它。 Here's example code that leads to the crash:这是导致崩溃的示例代码:

from tkinter import messagebox
messagebox.showerror('a','b')

Running this from the python interpreter (at ~/miniforge3/bin/ ) works, but from ipython (same path) causes a crash back to terminal:从 python 解释器(在~/miniforge3/bin/ )运行它可以工作,但是从 ipython (相同路径)会导致崩溃回到终端:

2022-04-21 14:19:39.873 python[56056:2463981] -[NSApplication macOSVersion]: unrecognized selector sent to instance 0x11d627510
2022-04-21 14:19:39.876 python[56056:2463981] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0x11d627510'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000192b76b08 __exceptionPreprocess + 240
    1   libobjc.A.dylib                     0x00000001928c1e14 objc_exception_throw + 60
    2   CoreFoundation                      0x0000000192c0aa68 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x0000000192ad5e14 ___forwarding___ + 1764
    4   CoreFoundation                      0x0000000192ad5670 _CF_forwarding_prep_0 + 96
    5   libtk8.6.dylib                      0x000000012c3ef918 GetRGBA + 264
    6   libtk8.6.dylib                      0x000000012c3ef728 TkpGetColor + 648
    7   libtk8.6.dylib                      0x000000012c33a1cc Tk_GetColor + 168
    8   libtk8.6.dylib                      0x000000012c32b4e0 Tk_Get3DBorder + 152
    9   libtk8.6.dylib                      0x000000012c32b320 Tk_Alloc3DBorderFromObj + 144
    10  libtk8.6.dylib                      0x000000012c33b77c DoObjConfig + 848
    11  libtk8.6.dylib                      0x000000012c33b334 Tk_InitOptions + 372
    12  libtk8.6.dylib                      0x000000012c33b218 Tk_InitOptions + 88
    13  libtk8.6.dylib                      0x000000012c3683e0 CreateFrame + 1480
    14  libtk8.6.dylib                      0x000000012c36870c TkListCreateFrame + 172
    15  libtk8.6.dylib                      0x000000012c360f68 Initialize + 1856
    16  _tkinter.cpython-39-darwin.so       0x000000012af6e078 Tkapp_New + 876
    17  _tkinter.cpython-39-darwin.so       0x000000012af6d9a0 _tkinter_create + 596
    18  python3.9                           0x00000001025f827c cfunction_vectorcall_FASTCALL + 208
    19  python3.9                           0x00000001026b3614 call_function + 572
    20  python3.9                           0x00000001026afd44 _PyEval_EvalFrameDefault + 29268
    21  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    22  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    23  python3.9                           0x00000001025a3468 _PyObject_FastCallDictTstate + 320
    24  python3.9                           0x00000001025a41e0 _PyObject_Call_Prepend + 164
    25  python3.9                           0x000000010261dbb4 slot_tp_init + 380
    26  python3.9                           0x000000010262c658 type_call + 344
    27  python3.9                           0x00000001025a31e8 _PyObject_MakeTpCall + 340
    28  python3.9                           0x00000001026b36ac call_function + 724
    29  python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    30  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    31  python3.9                           0x00000001026b3614 call_function + 572
    32  python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    33  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    34  python3.9                           0x00000001026b3614 call_function + 572
    35  python3.9                           0x00000001026afd44 _PyEval_EvalFrameDefault + 29268
    36  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    37  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    38  python3.9                           0x00000001026b3614 call_function + 572
    39  python3.9                           0x00000001026afd44 _PyEval_EvalFrameDefault + 29268
    40  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    41  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    42  python3.9                           0x00000001025a3468 _PyObject_FastCallDictTstate + 320
    43  python3.9                           0x00000001025a41e0 _PyObject_Call_Prepend + 164
    44  python3.9                           0x000000010261dbb4 slot_tp_init + 380
    45  python3.9                           0x000000010262c658 type_call + 344
    46  python3.9                           0x00000001025a31e8 _PyObject_MakeTpCall + 340
    47  python3.9                           0x00000001026b36ac call_function + 724
    48  python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    49  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    50  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    51  python3.9                           0x00000001026b3614 call_function + 572
    52  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    53  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    54  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    55  python3.9                           0x00000001026b0078 _PyEval_EvalFrameDefault + 30088
    56  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    57  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    58  python3.9                           0x00000001026b3614 call_function + 572
    59  python3.9                           0x00000001026afd44 _PyEval_EvalFrameDefault + 29268
    60  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    61  python3.9                           0x00000001026a3020 builtin_exec + 1032
    62  python3.9                           0x00000001025f827c cfunction_vectorcall_FASTCALL + 208
    63  python3.9                           0x00000001026b3614 call_function + 572
    64  python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    65  python3.9                           0x00000001025ba04c gen_send_ex + 532
    66  python3.9                           0x00000001026ac37c _PyEval_EvalFrameDefault + 14476
    67  python3.9                           0x00000001025ba04c gen_send_ex + 532
    68  python3.9                           0x00000001026ac37c _PyEval_EvalFrameDefault + 14476
    69  python3.9                           0x00000001025ba04c gen_send_ex + 532
    70  python3.9                           0x00000001025b0cbc method_vectorcall_O + 160
    71  python3.9                           0x00000001026b3614 call_function + 572
    72  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    73  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    74  python3.9                           0x00000001026b3614 call_function + 572
    75  python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    76  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    77  python3.9                           0x00000001026b3614 call_function + 572
    78  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    79  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    80  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    81  python3.9                           0x00000001025a6cf0 method_vectorcall + 164
    82  python3.9                           0x00000001026b3614 call_function + 572
    83  python3.9                           0x00000001026afe40 _PyEval_EvalFrameDefault + 29520
    84  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    85  python3.9                           0x00000001026b3614 call_function + 572
    86  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    87  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    88  python3.9                           0x00000001026b3614 call_function + 572
    89  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    90  python3.9                           0x00000001025a3ee4 function_code_fastcall + 116
    91  python3.9                           0x00000001026b3614 call_function + 572
    92  python3.9                           0x00000001026afd28 _PyEval_EvalFrameDefault + 29240
    93  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    94  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    95  python3.9                           0x00000001025a6cf0 method_vectorcall + 164
    96  python3.9                           0x00000001025a3a5c PyVectorcall_Call + 156
    97  python3.9                           0x00000001026b0078 _PyEval_EvalFrameDefault + 30088
    98  python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    99  python3.9                           0x00000001025a3e64 _PyFunction_Vectorcall + 240
    100 python3.9                           0x00000001026b3614 call_function + 572
    101 python3.9                           0x00000001026afdc4 _PyEval_EvalFrameDefault + 29396
    102 python3.9                           0x00000001026a84a8 _PyEval_EvalCode + 2968
    103 python3.9                           0x000000010270b834 pyrun_file + 376
    104 python3.9                           0x000000010270ad48 PyRun_SimpleFileExFlags + 816
    105 python3.9                           0x000000010272de84 Py_RunMain + 2916
    106 python3.9                           0x000000010272f018 pymain_main + 1272
    107 python3.9                           0x0000000102549ddc main + 56
    108 dyld                                0x0000000102a75088 start + 516
)
libc++abi: terminating with uncaught exception of type NSException
Abort trap: 6

Just a call to tkinter.Tk() gives the same.只需调用tkinter.Tk()即可。 I get the same if I use python --gui 'tk' .如果我使用python --gui 'tk' ,我会得到相同的结果。 Any idea how to solve or at least debug this?知道如何解决或至少调试这个吗?

Edit:编辑:
I just found this answer which reminded me that I import matplotlib.pyplot upon the startup of ipython .我刚刚找到这个答案,它提醒我在matplotlib.pyplot启动时导入了ipython Turning this off fixed it.关闭它可以修复它。 But if someone can elaborate why this is or how to really fix it, that would be greatly appreciated!但是,如果有人可以详细说明为什么会这样或如何真正解决它,那将不胜感激!

I found that I had to explicitly set matplotlib to use TK:我发现我必须明确设置matplotlib才能使用 TK:

import matplotlib
matplotlib.use("TkAgg")

My hypothesis is that IPython loads matplotlib, and that MPL by default uses a different UI backend, and that loading two UI backends causes trouble.我的假设是 IPython 加载 matplotlib,而 MPL 默认使用不同的 UI 后端,加载两个 UI 后端会导致麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM