Tkinter method Tk() failed with error message

Issue #201 invalid
created an issue

When I called the Tkinter method Tk(), I see this error message:

-[NSApplication _setup:]: unrecognized selector sent to instance

A window is displayed but none of the widgets are in it. When I resize the window it changes color from white to black.

Here is what the code I use look like:

mywindow = Tk()
label = Label(mywindow, text="Hi")

Comments (10)

  1. Ronald Oussoren repo owner

    In what way?

    PyObjC provides bindings to the Cocoa framework on macOS and has no specific support for Tkinter (and there's also no particular reason why you couldn't use both Tkinter and PyObjC in the same program).

    Your report contains no information that I can use to help your w.r.t. PyObjC (and I know too little about Tkinter to be able to help you with Tkinter specific issues)

  2. T M reporter

    I was hoping to make a Tkinter window display when I pushed a button.

    You can try this code that was connected to a button:

    def decrement_(self, sender):
            mywindow = Tk()
            label = Label(mywindow, text="Hi")
            button = Button(mywindow, text="Button")
        except Exception as e:
            print("Error: " + str(e))
  3. Ronald Oussoren repo owner

    That's explains the connection to PyObjC. Thanks.

    I don't have time to fully investigate this, but looking at the Tk source code and in particular the files tkMacOSXInit.c and tkMacOSXPrivate.h it appears that Tk defines a custom (private) subclass of NSApplication and assumes that this is the shared application (NSApplication.sharedApplication()). This private NSApplication subclass defines an _setup method, while the regular NSApplication class does not.

    If my analysis is correct there's not much I can do about this. But that said, my analysis is based on quickly browsing some source code and might not be correct.

    I'm curious though: why do you want to use Tk from a Cocoa script?

  4. Ronald Oussoren repo owner

    I've asked the pythonmac-sig mailinglist if someone knows a way to integrate Tkinter into a Cocoa program.

    One thing to try is to create the Tk() object before starting the main Cocoa runloop.

  5. T M reporter

    I wanted to make a python application that ran like a full Mac OS X application. I tired py2app but the resulting application was very big compared to an objective-c application. I was hoping PyObjC would be better. Thank you for all your help.

  6. Ronald Oussoren repo owner

    The application bundles generated using py2app don't require the use of PyObjC, and should work with plain Tkinter as well.

    The size of application bundles generated by py2app is mostly due to copying required resources (such as the python interpreter) into the application bundle and that's not something you can easily avoid with Python apps.

    A true Cocoa application using PyObjC should be slightly smaller when using py2app because the app bundle than doesn't need to contain a copy of Tcl/Tk.

    BTW. Using the system install of python (/usr/bin/python) should generate even smaller bundles when using py2app, but those generally only work on the same release of macOS (or later).

  7. Log in to comment