| 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:
This causes
The script uses both tkinter and GTK at the same time.
The line
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
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:
I am not sure what you are talking about. The TKApplication subclass does not override any NSApplication method. Look at the code:
I don't see any NSApplication methods being overridden there.
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 Methods to overrideGenerally, 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:
anonymous added on 2026-06-05 00:21:41:
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:
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? | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki