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

Windows: "PyEval_RestoreThread: NULL tstate" in tkinter example #535

Closed
ryanalder opened this issue Sep 11, 2019 · 14 comments
Closed

Windows: "PyEval_RestoreThread: NULL tstate" in tkinter example #535

ryanalder opened this issue Sep 11, 2019 · 14 comments

Comments

@ryanalder
Copy link

Steps to reproduce:

  1. Windows 10, Python 3.7.3 64bit, Tk 8.6.9, CEF Python 66
  2. Start the tkinter_.py example
  3. Click anywhere in the url_entry box
  4. Click anywhere on the webpage

I have tested this on two machines (same software versions). 100% reproducible for me so far. Doesn't matter when during execution this happens, even after using the browser for a while if you repeat steps 3 and 4 it will crash.

[tkinter_.py] CEF Python 66.0
[tkinter_.py] Python 3.7.3 64bit
[tkinter_.py] Tk 8.6.9

DevTools listening on ws:https://127.0.0.1:52580/devtools/browser/d3aa4707-2545-4c15-b260-80fd93f91746
Fatal Python error: PyEval_RestoreThread: NULL tstate

Current thread 0x00001da0 (most recent call first):
  File "C:\Users\ryana\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "tkinter_.py", line 60 in main
  File "tkinter_.py", line 385 in <module>
@cztomczak
Copy link
Owner

Tkinter example was tested using Tk 8.5 on Windows and Tk 8.6 on Linux. You can try downgrading Tk version and see if the issue reproduces.

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

I see the same issue:
[tkbrowser.py] CEF Python 66.0
[tkbrowser.py] Python 3.7.1 64bit
[tkbrowser.py] Tk 8.6.8
Fatal Python error: PyEval_RestoreThread: NULL tstate
.....
Current thread 0x00003114 (most recent call first):
File "C:\Program Files\Python37\lib\tkinter_init_.py", line 1283 in mainloop
File "j:\X-Plane Common\WinterSceneryProject\tkbrowser.py", line 60 in main
File "j:\X-Plane Common\WinterSceneryProject\tkbrowser.py", line 385 in

commenting the call to self.browser_frame.focus_set() in:

def OnGotFocus(self, **_):
    """Fix CEF focus issues (#255). Call browser frame's focus_set
       to get rid of type cursor in url entry widget."""
    logger.debug("FocusHandler.OnGotFocus")
    # self.browser_frame.focus_set()

solves the problem.

@cztomczak
Copy link
Owner

What about focus issues when you comment that code out?

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

Hi Czarek,
no issue in your example script with steeling the focus. However, in my integration where I attach the browser to a tk.Frame instead of the tk Window, I need to click out side of the application to enable any interaction with any native tk widget, very strange.
root = tk.Tk()
root.geometry("600x400")
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=tk.YES)
frame.text = tk.Entry(frame)
frame.text.pack()
# cef.Initialize()
browser = MainFrame(frame)
cef.Initialize()
browser.mainloop()
cef.Shutdown()

@cztomczak
Copy link
Owner

See Issue #255 and in source code comments that reference that issue. See any "focus" calls in the example and make it work similarly in your app. You can also try running with Tk 8.5 which doesn't throw the "NULL tstate" error.

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

Thanks a lot, I looked #255 already. Will try to mimic all of the focus calls from your example and report back, once successfull.

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

Ok. if found the 2 pieces to make it work, thank you very much!!!

class MainFrame(tk.Frame):
    ....
  
    def _on_focus_out(self, _):
        LOGGER.debug("MainFrame.on_focus_out")
        self.master.master.focus_force() # <- added
class BrowserFrame(tk.Frame):

    def __init__(self, master, url):
        """Initialize the browser frame."""
        # self.navigation_bar = navigation_bar
        self.closing = False
        self.browser = None
        self.url = url
        tk.Frame.__init__(self, master)
        self.pack(fill=tk.BOTH, expand=tk.YES)
        self.bind("<FocusIn>", self._on_focus_in)
        self.bind("<FocusOut>", self._on_focus_out)
        self.bind("<Configure>", self._on_configure)
        self.focus_set()
        self.master.master.focus_force() # <- added

The browser is attached to a tk.Notebook in my application:

ROOT.notebook = ttk.Notebook(ROOT)
ROOT.notebook.enable_traversal()
ROOT.notebook.pack(FRAME_PACK_ARGS)

TAB1FRAME = ttk.Frame(ROOT.notebook)
TAB1FRAME.pack(FRAME_PACK_ARGS)
.....

TAB2FRAME = browser.MainFrame(ROOT.notebook, "file:https:///scenerymap.html")
ROOT.notebook.add(TAB2FRAME, text=" Scenery Map ")
.....

This works now smoothly, even with the changes to the focus handler reverted:

class _FocusHandler():

    def __init__(self, browser_frame):
        self.browser_frame = browser_frame

    def OnTakeFocus(self, next_component, **_):
        LOGGER.debug("FocusHandler.OnTakeFocus, next={%1}".format(next=next_component))

    def OnSetFocus(self, source, **_):
        LOGGER.debug("FocusHandler.OnSetFocus, source={source}"
                     .format(source=source))
        return True

    def OnGotFocus(self, **_):
        """Fix CEF focus issues (#255). Call browser frame's focus_set
           to get rid of type cursor in url entry widget."""
        LOGGER.debug("FocusHandler.OnGotFocus")
        self.browser_frame.focus_set() # <- back to the reference implementation

@cztomczak
Copy link
Owner

Can you reproduce the issue with the official example originally reported by ryanalder here in first comment?

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

In your original implementation I can. I will make the 2 changes and report back

class BrowserFrame(tk.Frame):
    def __init__(self, master, url):
       ....
        self.master.master.focus_force() # <- added

and

class MainFrame(tk.Frame):
....
    def _on_focus_out(self, _):
        LOGGER.debug("MainFrame.on_focus_out")
        self.master.master.focus_force() # <- added

@ckn12345
Copy link

ckn12345 commented Jan 3, 2020

It works without steeling focus, however the call in

class FocusHandler(object):
....

    def OnGotFocus(self, **_):
        """Fix CEF focus issues (#255). Call browser frame's focus_set
           to get rid of type cursor in url entry widget."""
        logger.debug("FocusHandler.OnGotFocus")
        # self.browser_frame.focus_set()

still needs to be commented out. I am attaching the example script.
tkbrowser.py.txt

@cztomczak
Copy link
Owner

Fixed in commits b7f25aa (Windows) and 076b6a6 (Linux).

@cztomczak
Copy link
Owner

In tkinter 8.5 on Windows when you drag the window during initial loading you can still see the Fatal Python error: PyEval_RestoreThread: NULL tstate error, however on tkinter 8.6 it works fine.

@cztomczak
Copy link
Owner

The "PyEval_RestoreThread: NULL tstate" kinds of errors may probably be fixed by using CEF multi-threaded message loop. See this comment: #441 (comment)

@ggoo34
Copy link

ggoo34 commented Feb 23, 2022

tkinter dragging window error

Fatal Python error: PyEval_RestoreThread: NULL tstate
Python runtime state: initialized

Current thread 0x00003b14 (most recent call first):
File "C:/Users/1/Downloads/cefpython-66.1/cefpython-66.1/examples/tkinter_.py", line 215 in message_loop_work
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 814 in callit
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 1892 in call
File "C:\Users\1r\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 1429 in mainloop
File "C:/Users/1/Downloads/cefpython-66.1/cefpython-66.1/examples/tkinter_.py", line 65 in main
File "C:/Users/1/Downloads/cefpython-66.1/cefpython-66.1/examples/tkinter_.py", line 429 in

Process finished with exit code -1073740791 (0xC0000409)

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

4 participants