Tk Source Code

View Ticket
Login
Ticket UUID: 637e74c1e7ba9bc440bd2710dc8f00baba3da1f1
Title: TKApplication unrecognized selector causes program to abort
Type: Bug Version: 8.6, 9.0
Submitter: anonymous Created on: 2026-06-04 01:10:48
Subsystem: 83. Mac OS X Build Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-08-19 09:37:48
Resolution: None Closed By: nobody
    Closed on:
Description:

This bug occurs on macOS only.

In tkMacOSXPrivate.h, Tk defines TKApplication as a subclass of NSApplication. This subclass is instantiated in tkMacOSXInit.c by calling [TKApplication sharedApplication]. This sets the global variable NSApp as an instance of TKApplication.

Other modules also have access to NSApp, and can set NSApp before Tk has a chance to initialize it. In that case, NSApp is defined as an instance of an NSApplication (or some other subclass of NSApplication).

Tk assumes that NSApp is a TKApplication, and uses it as such. This can cause the program to abort.

This is one example from tkinter:

import tkinter

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk

root = tkinter.Tk()

This causes

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0xc24f34300'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000018d0091c0 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x000000018ca9291c objc_exception_throw + 88
	2   CoreFoundation                      0x000000018d0ca250 -[NSObject(NSObject) __retain_OA] + 0
	3   CoreFoundation                      0x000000018cf79f38 ___forwarding___ + 1480
	4   CoreFoundation                      0x000000018cf798b0 _CF_forwarding_prep_0 + 96
	5   Tk                                  0x0000000107fdab9c TkSetMacColor + 2940
	6   Tk                                  0x0000000107fda9cc TkSetMacColor + 2476
...
)
libc++abi: terminating due to uncaught exception of type NSException
zsh: abort      python test.py
The script uses both tkinter and GTK at the same time. The line
from gi.repository import Gtk
initializes NSApp as a bare NSApplication object. When Tk creates its window, Tk sends [NSApplication macOSVersion] to NSApp; this selector is defined for the subclass TKApplication. But since NSApp is now a bare NSApplication, the selector is not recognized, causing the program to abort.

The bug can be avoided by creating the Tk window before GTK attempts to instantiate NSApplication, as in

import tkinter

import gi
gi.require_version('Gtk', '3.0')

root = tkinter.Tk()

from gi.repository import Gtk

In this case, Tk initializes NSApp as a TKApplication.

As NSApp is a global variable that can be set and accessed from any module, it is probably better not to subclass it at all.

I believe fixing this bug is fairly easy, as it just requires reorganizing some code. I am happy to submit a patch, if there is sufficient interest.

User Comments: jan.nijtmans added on 2026-08-19 09:37:48:

> You do not know which change I am proposing

I would like to hear from you what change you are proposing. It might help in clearing up the confusion. Maybe it's not possible, but without a specific proposal there's no way to know. If it helps tkinter users preventing an abort, it's worth to have a look at it. But we need your help.

If you cannot (or don't want to) provide that, I propose to close this ticket.


anonymous added on 2026-07-02 00:05:12:
> The bottom line is that we do not support the change you are proposing because it touches the foundations of Tk’s design on macOS.

I have not yet submitted a patch. You do not know which change I am proposing.

anonymous added on 2026-07-02 00:03:59:
> Incidentally, there are many methods of NSApplication which are overridden. You do not see these overridden methods in the header file because there is no need to declare them. You need to look in the @implementation blocks, where you find the code for overridden methods.

Yes I know. But I am not talking about those. Those are added via categories, which is a different thing from subclassing. You can avoid subclassing while still overriding those methods via categories. Then users can avoid the crash I reported.

marc_culler (claiming to be Marc Culler) added on 2026-06-25 21:37:25:
Incidentally, there are many methods of NSApplication which are
overridden.  You do not see these overriddent methods in the header file
because there is no need to declare them.  You need to look in the
@implementation blocks, where you find the code for overridden methods.

kevin_walzer added on 2026-06-18 02:36:33:
The bottom line is that we do not support the change you are proposing because it touches the foundations of Tk’s design on macOS.

anonymous added on 2026-06-18 01:18:13:

The macOS Tk port qualifies as an application which needs to override NSApplication methods.

I am not sure what you are talking about. The TKApplication subclass does not override any NSApplication method. Look at the code:

@interface TKApplication : NSApplication {
@private
    Tcl_Interp *_eventInterp;
    NSMenu *_servicesMenu;
    TKMenu *_defaultMainMenu, *_defaultApplicationMenu;
    NSMenuItem *_demoMenuItem;
    NSArray *_defaultApplicationMenuItems, *_defaultWindowsMenuItems;
    NSArray *_defaultHelpMenuItems, *_defaultFileMenuItems;
    NSAutoreleasePool *_mainPool;
}
@property int poolLock;
@property int macOSVersion;
@property Bool tkLiveResizeEnded;
@property Bool tkWillExit;

/*
 * Persistent state variables used by processMouseEvent.
 */

@property(nonatomic) TkWindow *tkPointerWindow;
@property(nonatomic) TkWindow *tkEventTarget;
@property(nonatomic) TkWindow *tkDragTarget;
@property unsigned int tkButtonState;

@end

I don't see any NSApplication methods being overridden there.

The story that completely changing everything about how events are handled will be "fairly easy" is not close to being true.

I fully agree with you that completely changing everything about how events are handled is not easy. But I am not suggesting that at all. In fact, I don't want to make any changes to how events are being handled.


marc_culler (claiming to be Marc Culler) added on 2026-06-05 01:52:52:
The macOS Tk port qualifies as an application which needs to override
NSApplication methods.  It never calls the run method, because it
can't, because it has to do its event handling in a way which is
compatible with the generic Tk event handling. The NSApplication needs
to be adapted so it functions as a Tcl event source. Tk runs on several
platforms and all of them need to use as much of the Tk generic code as
possible.

I am not opening this can of worms.  The story that completely changing
everything about how events are handled will be "fairly easy" is not
close to being true.

anonymous added on 2026-06-05 00:33:00:
> If you want to use GTk, use GTk.  If you want to use Tk, use Tk.

I don't have control over that. A user may want to use one module that does GUI stuff with GTK, and another module that does GUI stuff with Tk.

anonymous added on 2026-06-05 00:31:12:

Also from Apple's documentation. They suggest to subclass NSApplication if you need to override methods related to the event loop, such as run, sendEvent:, requestUserAttention:, targetForAction:. TKApplication doesn't do any of that.

Methods to override

Generally, you subclass NSApplication to provide your own special responses to messages that are routinely sent to the global app object (NSApp). NSApplication doesn’t have primitive methods in the sense of methods that you must override in your subclass. Here are four methods that are possible candidates for overriding:

  • Override run if you want the app to manage the main event loop differently than it does by default. (This a critical and complex task, however, that you should only attempt with good reason).

  • Override sendEvent: if you want to change how events are dispatched or perform some special event processing.

  • Override requestUserAttention: if you want to modify how your app attracts the attention of the user (for example, offering an alternative to the bouncing app icon in the Dock).

  • Override targetForAction: to substitute another object for the target of an action message.


anonymous added on 2026-06-05 00:21:41:

Subclassing NSApplication is completely standard and recommended by Apple.

Actually Apple does not recommend it.

Citing from Apple's documentation:

Subclassing notes

You rarely should find a real need to create a custom NSApplication subclass. Unlike some object-oriented libraries, Cocoa doesn’t require you to subclass NSApplication to customize app behavior. Instead it gives you many other ways to customize an app. This section discusses both some of the possible reasons to subclass NSApplication and some of the reasons not to subclass NSApplication.


marc_culler (claiming to be Marc Culler) added on 2026-06-04 16:51:00:
I did a little research on this.  Actually, GTk does not subclass
NSApplication.  Instead they create a separate class which is appointed as
the Delegate for their singleton NSApplication object.  That is probably
a more modern approach.  The TKApplication subclass goes back to the
initial Cocoa Port of Tk about 20 years ago.

It would probably be possible for Tk to use a separate Delegate instead
of a subclass.  Converting the code to do that would be a huge project,
however, and I don't think it would resolve this problem.  There would
still be two delegates with different methods and only one of them could
be assigned to the singleton NSApplication object.

I am not interested in making that change, and I am infinitely less
interested in being asked to review a monstrous patch created by Claude
which purports to make that change -- especially given that I don't think
it would solve this "problem", which I do not view as a problem at all.
If you want to use GTk, use GTk.  If you want to use Tk, use Tk.

kevin_walzer added on 2026-06-04 15:59:49:
But I don’t believe this is a use case we want to support. Ultimately both Gtk and Tk are trying to inherit from the same core object. Thst’s a platform-specific issue that is not present on X11.

anonymous added on 2026-06-04 13:55:18:

You wouldn’t be able to mix Gtk and Tk on Linux without a lot of headaches and unpredictable results.

Sure you can, and I have done it. Tcl supports the use of external event loops through Tcl_SetNotifier. All you need to do is to set up GLib's event loop as an external event loop for Tcl. It is not easy, but Tcl provides the API for it, and if you use it correctly, it works like a charm.


marc_culler (claiming to be Marc Culler) added on 2026-06-04 02:09:36:
Subclassing NSApplication is completely standard and recommended by Apple.
Gtk almost surely has its own subclass.

It is not allowed for a process to instantiate two NSApplication objects,
and that is what you are doing.

kevin_walzer added on 2026-06-04 01:42:22:
The global singleton NSApp object is a fundamental part of Cocoa development. On the other hand, mixing GUI frameworks is unsupported pretty much everywhere. You wouldn’t be able to mix Gtk and Tk on Linux without a lot of headaches and unpredictable results. What are you trying to accomplish?