Tk Source Code

View Ticket
Login
Ticket UUID: 3033786
Title: Cannot embed Tk in a Cocoa Application
Type: Bug Version: None
Submitter: silverjamx Created on: 2010-07-23 21:46:57
Subsystem: 83. Mac OS X Build Assigned To: aku
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2018-09-20 13:28:26
Resolution: None Closed By: nobody
    Closed on: 2011-01-17 15:39:33
Description:
Tk built against Cocoa does not allow Tk to be embedded in another Cocoa application.  This is apparent when embedding Python inside of a Cocoa application.  The tkinter module does not work.

I think the problem stems from the fact that the Tk initialization code subclasses NSApplication.  If another application gets a chance to create an NSApplication object errors like this are thrown:

2010-07-23 15:30:59.721 X-Chat Aqua[32706:903] -[SGApplication _setup:]: unrecognized selector sent to instance 0x606aa0
2010-07-23 15:30:59.722 X-Chat Aqua[32706:903] Exception raised during posting of notification.  Ignored.  exception: \'-[SGApplication _setup:]: unrecognized selector sent to 

See the attached example to reproduce the problem (requires Xcode).

I\'ve found at least two applications that use Python scripting on the Mac that are broken with respect to being able to use Tk.  Our application works on 10.5 (since Tk there is built against Carbon).

http://golly.sourceforge.net/
https://sourceforge.net/projects/xchataqua/

These are both open source applications, but any application that uses Python scripting is probably broken (see http://wiki.python.org/moin/AppsWithPythonScripting).
User Comments: kjnash added on 2018-09-20 13:28:26:
Perhaps someone else will consider adding this feature.  I would attempt it myself if I had sufficient skill at C and Objective C.

If the absence of embedding is not recognized as a bug, I would suggest that the man pages frame(n), toplevel(n), and safe_loadTk(n) should be amended to note that the feature is not available on the aqua windowing system.

kevin_walzer added on 2018-09-19 10:46:57:
As das said some years ago, the technical issues involved here are formidable, perhaps insurmountable, and I have no plans to invest further time in this.  Is there any reason to leave this bug open if no action will be taken on it?

kjnash added on 2018-09-19 08:15:30:
My particular application is entirely Tcl/Tk - embedding a slave interpreter or a Tcl/Tk child process.

I've posted here because this ticket discusses the difficulties with embedding in Cocoa, and because my own ticket is listed as a duplicate of this one.

https://core.tcl.tk/tk/tktview?name=3607262

kevin_walzer added on 2018-09-19 02:19:57:
Wouldn't it be possible to embed Python compiled without Tkinter support into a Cocoa application using its normal embedding API? If the goal is to provide a Python scripting interface, that would sidestep the problem of clashing GUI frameworks/event loops altogether.

kjnash added on 2018-09-18 13:36:17:
Apologies for re-opening this bug, which was previously labeled "Won't Fix".

I note the comment made by das on 2010-07-24, and I can see that there are substantial difficulties with embedding one arbitrary window in another.

However, many practical usage cases do not involve contention over menus - for example, embedding the window of a slave interpreter or worker process.  A developer might wish to render a web page in a separate process whose window is embedded in a window of the main process.

In these cases, the menubar should remain with the main process, and the problem of coordinating the menubars of two windows does not occur.

wordtech added on 2011-01-17 22:39:33:

allow_comments - 1

silverjamx added on 2010-07-24 06:09:35:
It worked fine with Carbon+Tk.  However...

I can accept it not being a goal but since Tk is the default GUI of Python it's a bit jarring to have the app crash when a user tries to do something with a GUI from an application scripting perspective.

das added on 2010-07-24 04:53:27:
Embedding Tk into a cocoa app is a non-goal IMO, it's unlikely that this could ever be made to work given the coordination with global objects and UI elements (such as the menubar) of the embedding app that would be necessary.

silverjamx added on 2010-07-24 04:46:58:

File Added - 380961: PyCocoaTk.zip

Attachments: