Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac: tkinter_.py example: error with winfo_id() returning a negative value #308

Closed
cztomczak opened this issue Mar 1, 2017 · 1 comment

Comments

@cztomczak
Copy link
Owner

cztomczak commented Mar 1, 2017

Looks like there is no way to get window id (NSView pointer) in tkinter on Mac, so that browser can be embedded in that window. From tk manual:

winfo id window
Returns a hexadecimal string giving a low-level platform-specific identifier for window. On Unix platforms, this is the X window identifier. Under Windows, this is the Windows HWND. On the Macintosh the value has no meaning outside Tk.

Reference: https://www.tcl.tk/man/tcl8.4/TkCmd/winfo.htm#M17

Below is the error when trying to embed CEF browser using that negative window identifier:

[tkinter_.py] winfo_id()=-1683723760
Exception in Tkinter callback
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 531, in callit
    func(*args)
  File "../examples/tkinter_.py", line 276, in embed_browser
    window_info.SetAsChild(self.winfo_id())
  File "thon/window_info.pyx", line 82, in cefpython_py27.WindowInfo.SetAsChild (cefpython.cpp:39561)
OverflowError: can't convert negative value to size_t
@cztomczak cztomczak changed the title Mac: tkinter example error because winfo_id() returns a negative value Mac: tkinter_.py example: error with winfo_id() returning a negative value Mar 1, 2017
cztomczak added a commit that referenced this issue Mar 1, 2017
Fix tkinter error on Mac "unrecognized selector sent to instance"
(Issue #306"). CEF must be initialized after Tk.

Fix error with winfo_id() returning a negative value (Issue #308).
This was resolved by obtaining NSView pointer using PyObjC
package.

There is yet another error "Segmentation fault: 11" which
crashes app often, however it's hard to debug it (Issue #309).
@cztomczak
Copy link
Owner Author

Fixed it by using PyObjC package to obtain NSView pointer:

    def get_window_handle(self):
        if self.winfo_id() > 0:
            return self.winfo_id()
        elif MAC:
            # On Mac window id is an invalid negative value (Issue #308).
            # This is kind of a dirty hack to get window handle using
            # PyObjC package. If you change structure of windows then you
            # need to do modifications here as well.
            # noinspection PyUnresolvedReferences
            from AppKit import NSApp
            # noinspection PyUnresolvedReferences
            import objc
            # Sometimes there is more than one window, when application
            # didn't close cleanly last time Python displays an NSAlert
            # window asking whether to Reopen that window.
            # noinspection PyUnresolvedReferences
            return objc.pyobjc_id(NSApp.windows()[-1].contentView())
        else:
            raise Exception("Couldn't obtain window handle")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant