pywebview: Can't open a new window from the JS API
Specification
- Platform: winforms
- Version: 2.0.3
Description
My code looks a bit like this:
import os
import threading
import webview
class FirstWindowApi:
def on_button(self):
webview.destroy_window()
t = threading.Thread(target=create_second_window)
t.start()
class SecondWindowApi:
pass
def create_second_window():
second_window = webview.create_window("Window 2", os.path.abspath("web\\window2.html"), js_api=SecondWindowApi)
first_window = webview.create_window("Window 1", os.path.abspath("web\\window1.html"), js_api=FirstWindowApi)
However, when I click the button, it crashes with this exception:
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Program Files\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "ui_pywebview.py", line 23, in create_second_window
second_window = webview.create_window("Window 2", os.path.abspath("web\\window2.html"), js_api=SecondWindowApi)
File "C:\Program Files\Python36\lib\site-packages\webview\__init__.py", line 183, in create_window
background_color, debug, js_api, text_select, _webview_ready)
File "C:\Program Files\Python36\lib\site-packages\webview\winforms.py", line 252, in create_window
BrowserView.instances['master'].Invoke(Func[Type](create))
KeyError: 'master'
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 22 (10 by maintainers)
@TheInitializer as your case could be solved by switching the order of destroy_window and create_window, I see no reason in keeping application loop alive. Besides how would you terminate the application loop if a keep-alive feature would be implemented?
The threading model should be probably be documented in README.
QT had the same problem as Windows with restarting application loop. All the platforms should be fixed by now.
This time it is the expected behaviour. This is the proper exception pywebview raises if you try to call any api function on a non-existent window, as mentioned in our README:
Just wrap your code within a
try-except
block and handle it yourself. Better than forcing your users to do something. 🙂@r0x0r It seems my patch fixes this issue. Can we merge it to master? Let us open a new issue for tracking the app-termination-behaviour.
Yes this is correct. Single window would always block control and release once closed.
Another related problem is that opening a second window after the closing the first one fails on Windows. Ie
On OSX it terminates the whole app after the window is closed. Windows and QT fail, as they try to create a child window after the app is terminated. The culprit is the
create_window
initialization code in init.py.I believe that the program should not terminate after the last window is closed, but instead give control to the user code and wait until it is finished executing. Opinions?