cefpython icon indicating copy to clipboard operation
cefpython copied to clipboard

TkInter: Windows - Dragging problems

Open nabeelio opened this issue 3 years ago • 15 comments

I'm trying to implement the browser window within a tkinter project, and I'm running into issues with the window drag not working properly (it just snaps back in place or it doesn't move), and can't resize either. It seems like it gets stuck. I can replicate this with the tkinter example. I'm on Windows 10, and seeing it on other Windows machines, too.

Hard to tell from the video, but here it is: https://www.youtube.com/watch?v=slX-Ijlp8bM

It seems like sometimes it does move, but maybe only once, then it's hard to get it to move from that position. My Google-fu hasn't been very fruitful, I've been trying all sorts of things, but nothing really helping

Thanks!

nabeelio avatar Oct 30 '20 11:10 nabeelio

Since it works in official tkinter example and doesn't seem related to CEF since window management is handled by tkinter, I can't see what is the CEF Python issue here.

cztomczak avatar Oct 31 '20 11:10 cztomczak

I'm not sure what you mean, which official example? The one in the examples directory doesn't work. Removing CEF Python from the window (not calling embed or whatever) fixes the dragging issue. Seeing the same thing in my app. The YouTube vid is the script in the example directory

nabeelio avatar Oct 31 '20 12:10 nabeelio

What tkinter and CEF versions? Anything interesting in logs?

cztomczak avatar Oct 31 '20 12:10 cztomczak

Sorry - I should have included the versions in the original post:

Connected to pydev debugger (build 202.7319.50)
[hello.py] CEF Python 66.0
[hello.py] Python 3.7.8 64bit
[hello.py] Tk 8.6.9

DevTools listening on ws://127.0.0.1:64265/devtools/browser/e68dbbcb-84d5-4c35-9480-23e5a2aa5e76
[hello.py] MainFrame.on_root_configure
[hello.py] MainFrame.on_configure
[hello.py] MainFrame.on_root_configure
[hello.py] MainFrame.on_root_configure
[hello.py] MainFrame.on_root_configure
...

Nothing interesting that I can see, though it eventually crashes (sometimes) as I keep trying to drag it:

Fatal Python error: PyEval_RestoreThread: NULL tstate

Thread 0x00004b2c (most recent call first):
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 166 in _on_run
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 216 in run
  File "C:\Python\Python37\lib\threading.py", line 926 in _bootstrap_inner
  File "C:\Python\Python37\lib\threading.py", line 890 in _bootstrap

Thread 0x00004b08 (most recent call first):
  File "C:\Python\Python37\lib\threading.py", line 300 in wait
  File "C:\Python\Python37\lib\threading.py", line 552 in wait
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 144 in _on_run
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 216 in run
  File "C:\Python\Python37\lib\threading.py", line 926 in _bootstrap_inner
  File "C:\Python\Python37\lib\threading.py", line 890 in _bootstrap

Thread 0x00004b70 (most recent call first):
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 290 in _on_run
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 216 in run
  File "C:\Python\Python37\lib\threading.py", line 926 in _bootstrap_inner
  File "C:\Python\Python37\lib\threading.py", line 890 in _bootstrap

Thread 0x00004b74 (most recent call first):
  File "C:\Python\Python37\lib\threading.py", line 300 in wait
  File "C:\Python\Python37\lib\queue.py", line 179 in get
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 365 in _on_run
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 216 in run
  File "C:\Python\Python37\lib\threading.py", line 926 in _bootstrap_inner
  File "C:\Python\Python37\lib\threading.py", line 890 in _bootstrap

Current thread 0x000049bc (most recent call first):
  File "C:/Users/Nabeel/dev/gebeyond/mavlink2amp/hello.py", line 217 in message_loop_work
  File "C:\Python\Python37\lib\tkinter\__init__.py", line 749 in callit
  File "C:\Python\Python37\lib\tkinter\__init__.py", line 1705 in __call__
  File "C:\Python\Python37\lib\tkinter\__init__.py", line 1283 in mainloop
  File "C:/Users/Nabeel/dev/gebeyond/mavlink2amp/hello.py", line 66 in main
  File "C:/Users/Nabeel/dev/gebeyond/mavlink2amp/hello.py", line 430 in <module>
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18 in execfile
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 1448 in _exec
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 1441 in run
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 2132 in main
  File "C:\Path\\IDEA-U\ch-0\202.7319.50.plugins\python\helpers\pydev\pydevd.py", line 2141 in <module>

I'm not sure what other logs to add. Sometimes it'll work and drag properly once or twice (meaning I can move it once maybe), but then it just gets stuck snapping.

I've also tried the disable-gpu and disable-gpu-compositing flags, didn't make a difference.

nabeelio avatar Oct 31 '20 13:10 nabeelio

Other problems related to "PyEval_RestoreThread" error message: https://github.com/cztomczak/cefpython/issues?q=is%3Aissue+is%3Aopen+PyEval_RestoreThread

cztomczak avatar Oct 31 '20 13:10 cztomczak

Yeah, I saw those, possibly related because they'd be sorta the same callback, but I think that's a separate problem from the window drag. Those changes are already in with the tkinter example script.

What other logging, etc, can I add?

nabeelio avatar Oct 31 '20 13:10 nabeelio

CEF somehow affects tkinter app state. These issues become more severe if you initialize CEF before you initialize tkinter (see comments in example). One idea to try is to enable CEF multi threaded message loop and see if this anyhow helps (see ApplicationSettings). When multi threaded message loop is enabled CEF UI thread is no more application main thread. Another thing worth trying is to move all CEF code to a separate process, but this would require significant amount of work, as you would have to wrap all functionlity using inter-process messaging.

cztomczak avatar Oct 31 '20 13:10 cztomczak

We might investigate and take a look at tkinter initializtion source codes to see what exactly it is doing, but that's not really in tkinter python source codes, but somewhere in Tcl/Tk source codes: https://github.com/tcltk .

cztomczak avatar Oct 31 '20 13:10 cztomczak

Oh wow, ok, that seems to have fixed it. Still with the example, changing:

settings = {}
if MAC:
    settings["external_message_pump"] = True

To just

settings = {
    "external_message_pump": True,
}

Does that have any other side effects? Should I add a PR changing that on the tkinter example?

nabeelio avatar Oct 31 '20 13:10 nabeelio

That is not multi threaded message loop. The option I've referred to is named "multi_threaded_message_loop" and may require other modification to the script as well, because when enabled some functions must be run on UI thread then. See PR #412 for reference.

The "external_message_pump" option when enabled runs message loop work a bit differently and when used then you shouldn't call the BrowserFrame.message_loop_work function (and the cef.MessageLoopWork). Comment out that function.

I don't think "external_message_pump" can fix the root of the issue. It's more probable that the message loop work started running at different times and it's just luck that the issue got hidden momentarily. Try with multi threaded message loop for a more reliable fix.

cztomczak avatar Oct 31 '20 15:10 cztomczak

For enabling multi threaded message loop see Issue #380 for some code examples.

cztomczak avatar Oct 31 '20 15:10 cztomczak

Got it, thank you so much for your help. I'm gonna dig into this and see if I can understand it properly. I think with Tk refactoring it in a way so that CreateBrowserSync is run on the main thread might be tricky

nabeelio avatar Oct 31 '20 19:10 nabeelio

Were you able to find a fix for this problem?

AngelosGuanANL avatar Oct 24 '22 19:10 AngelosGuanANL

@AngelosGuanANL hey, this was two years ago, so I don't remember exactly but reading through the thread it kind of rings a bell. I think the message pump thing is what helped. It was at an old job, so I don't have the code anymore to reference

nabeelio avatar Oct 24 '22 22:10 nabeelio

@AngelosGuanANL hey, this was two years ago, so I don't remember exactly but reading through the thread it kind of rings a bell. I think the message pump thing is what helped. It was at an old job, so I don't have the code anymore to reference

Thank you! I fixed the dragging issue and random crashing when being dragged issue using the external_message_pump in the setting (settings["external_message_pump"] = True) and commenting out BrowserFrame.message_loop_work().

My BrowserFrame is not the mainframe in tkinter. I think this might have caused this issue, maybe using BrowerFrame as the root frame can avoid this issue.

AngelosGuanANL avatar Nov 11 '22 17:11 AngelosGuanANL