Skip to content

[Bug]: libc++abi: terminating due to uncaught exception of type NSException crash when combining tkinter and matplotlib with the macosx backend #30388

@ortk95

Description

@ortk95

Bug summary

If I create a matplotlib figure using pyplot (e.g. plt.subplots), then create a tkinter window, my code crashes with the libc++abi: terminating due to uncaught exception of type NSException error. This happens with the macosx backand, while the tkagg does not have any errors.

Code for reproduction

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
import matplotlib
matplotlib.use('macosx') # no error if the tkagg backend is used
import matplotlib.pyplot as plt
print(matplotlib.get_backend())
fig, ax = plt.subplots(figsize=(10, 10))
root = tk.Tk()

Actual outcome

macosx
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0x1397a7060'
*** First throw call stack:
(
        0   CoreFoundation                      0x000000019e02eca0 __exceptionPreprocess + 176
        1   libobjc.A.dylib                     0x000000019daf2b90 objc_exception_throw + 88
        2   CoreFoundation                      0x000000019e0ebb90 -[NSObject(NSObject) __retain_OA] + 0
        3   CoreFoundation                      0x000000019df9cddc ___forwarding___ + 1500
        4   CoreFoundation                      0x000000019df9c740 _CF_forwarding_prep_0 + 96
        5   libtk8.6.dylib                      0x0000000101159918 GetRGBA + 64
        6   libtk8.6.dylib                      0x0000000101159714 TkpGetColor + 428
        7   libtk8.6.dylib                      0x00000001010a37bc Tk_GetColor + 168
        8   libtk8.6.dylib                      0x0000000101094b7c Tk_Get3DBorder + 152
        9   libtk8.6.dylib                      0x00000001010949bc Tk_Alloc3DBorderFromObj + 144
        10  libtk8.6.dylib                      0x00000001010a4e08 DoObjConfig + 1008
        11  libtk8.6.dylib                      0x00000001010a4918 Tk_InitOptions + 372
        12  libtk8.6.dylib                      0x00000001010a47fc Tk_InitOptions + 88
        13  libtk8.6.dylib                      0x00000001010d20f8 CreateFrame + 1472
        14  libtk8.6.dylib                      0x00000001010d2424 TkListCreateFrame + 172
        15  libtk8.6.dylib                      0x00000001010cacbc Initialize + 1888
        16  _tkinter.cpython-313-darwin.so      0x0000000100d661d0 Tcl_AppInit + 92
        17  _tkinter.cpython-313-darwin.so      0x0000000100d65e20 Tkapp_New + 652
        18  _tkinter.cpython-313-darwin.so      0x0000000100d65b90 _tkinter_create_impl + 268
        19  _tkinter.cpython-313-darwin.so      0x0000000100d657f8 _tkinter_create + 276
        20  python3.13                          0x00000001005955d0 cfunction_vectorcall_FASTCALL + 96
        21  python3.13                          0x00000001006b4380 _PyEval_EvalFrameDefault + 45724
        22  python3.13                          0x00000001005d95e8 slot_tp_init + 336
        23  python3.13                          0x00000001005cc14c type_call + 148
        24  python3.13                          0x00000001006b482c _PyEval_EvalFrameDefault + 46920
        25  python3.13                          0x00000001006a6a98 PyEval_EvalCode + 148
        26  python3.13                          0x000000010076ff70 run_eval_code_obj + 144
        27  python3.13                          0x000000010076f92c run_mod + 168
        28  python3.13                          0x000000010076f754 pyrun_file + 144
        29  python3.13                          0x000000010076f014 _PyRun_SimpleFileObject + 256
        30  python3.13                          0x000000010076e8d8 _PyRun_AnyFileObject + 80
        31  python3.13                          0x000000010079bd18 pymain_run_file_obj + 180
        32  python3.13                          0x000000010079b364 pymain_run_file + 72
        33  python3.13                          0x000000010079a654 Py_RunMain + 1036
        34  python3.13                          0x000000010079c08c pymain_main + 560
        35  python3.13                          0x00000001004a5b30 main + 56
        36  dyld                                0x000000019db32b98 start + 6076
)
libc++abi: terminating due to uncaught exception of type NSException
Abort trap: 6

Expected outcome

Code should exit cleanly with no error

Additional information

The tkagg backend does not cause a crash:

import tkinter as tk
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
print(matplotlib.get_backend())
fig, ax = plt.subplots(figsize=(10, 10))
root = tk.Tk()

Manually creating a figure with Figure does not cause a crash either:

import tkinter as tk
import matplotlib
matplotlib.use('macosx')
import matplotlib.figure
import matplotlib.pyplot as plt
print(matplotlib.get_backend())
fig = matplotlib.figure.Figure()
ax = fig.add_subplot()
root = tk.Tk()

Operating system

OS/X

Matplotlib Version

3.10.0

Matplotlib Backend

macosx

Python version

Python 3.13.2

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions