LoginSignup
7
6

More than 5 years have passed since last update.

tkinterがMacで動かない

Last updated at Posted at 2019-03-27

tkinterでハマるポイントがありましたので、記録です。

import tkinter as tk
import ...

root = tk.Tk() # なぜかここで落ちる

PythonのtkinterでGUIを作るとき、Windowsでは動くのに、以下のようなエラーが出てMacでは動かない場合があります。

2019-03-27 23:05:40.010 python[1384:51780] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f8d42a81cf0
2019-03-27 23:05:40.014 python[1384:51780] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f8d42a81cf0'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff2e090cf9 __exceptionPreprocess + 256
        1   libobjc.A.dylib                     0x00007fff58c25a17 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff2e10ab06 -[NSObject(NSObject) __retain_OA] + 0
        3   CoreFoundation                      0x00007fff2e0330ef ___forwarding___ + 1485
        4   CoreFoundation                      0x00007fff2e032a98 _CF_forwarding_prep_0 + 120
        5   libtk8.6.dylib                      0x000000010a44331d TkpInit + 413
        6   libtk8.6.dylib                      0x000000010a39b17e Initialize + 2622
        7   _tkinter.cpython-36m-darwin.so      0x00000001096fca16 _tkinter_create + 1174
        8   python                              0x000000010879b068 _PyCFunction_FastCallDict + 200
        9   python                              0x000000010887061f call_function + 143
        10  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        11  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        12  python                              0x00000001088712cc _PyFunction_FastCallDict + 364
        13  python                              0x0000000108719f80 _PyObject_FastCallDict + 320
        14  python                              0x00000001087415f8 method_call + 136
        15  python                              0x00000001087215ce PyObject_Call + 62
        16  python                              0x00000001087c25b5 slot_tp_init + 117
        17  python                              0x00000001087c6af1 type_call + 241
        18  python                              0x0000000108719ef1 _PyObject_FastCallDict + 177
        19  python                              0x0000000108870718 call_function + 392
        20  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        21  python                              0x00000001088709dc fast_function + 188
        22  python                              0x000000010887067c call_function + 236
        23  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        24  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        25  python                              0x000000010885c129 builtin_exec + 345
        26  python                              0x000000010879b068 _PyCFunction_FastCallDict + 200
        27  python                              0x000000010887061f call_function + 143
        28  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        29  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        30  python                              0x0000000108870a8a fast_function + 362
        31  python                              0x000000010887067c call_function + 236
        32  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        33  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        34  python                              0x0000000108870a8a fast_function + 362
        35  python                              0x000000010887067c call_function + 236
        36  python                              0x000000010886e225 _PyEval_EvalFrameDefault + 47013
        37  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        38  python                              0x0000000108870a8a fast_function + 362
        39  python                              0x000000010887067c call_function + 236
        40  python                              0x000000010886e225 _PyEval_EvalFrameDefault + 47013
        41  python                              0x00000001088709dc fast_function + 188
        42  python                              0x000000010887067c call_function + 236
        43  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        44  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        45  python                              0x0000000108870a8a fast_function + 362
        46  python                              0x000000010887067c call_function + 236
        47  python                              0x000000010886e175 _PyEval_EvalFrameDefault + 46837
        48  python                              0x00000001088618c9 _PyEval_EvalCodeWithName + 425
        49  python                              0x00000001088ba55c PyRun_FileExFlags + 252
        50  python                              0x00000001088b9a34 PyRun_SimpleFileExFlags + 372
        51  python                              0x00000001088e07c6 Py_Main + 3734
        52  python                              0x0000000108711f59 main + 313
        53  libdyld.dylib                       0x00007fff5a4533d5 start + 1
        54  ???                                 0x0000000000000009 0x0 + 9
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

その場合には、以下のコードを書き加えると、動くようになりました。matplotlibは直接使用していないのですが、どこかで使用されているようです。

import matplotlib
matplotlib.use('tkagg')
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6