- Fixed an issue which caused JavaScript variables of the same name to be declared twice.

- Refactoring to allow the widget classes to use the subscribe_click method.
This commit is contained in:
louisnw
2023-05-17 12:45:54 +01:00
parent 993fbe8ed8
commit 88c8a266ec
5 changed files with 75 additions and 52 deletions

View File

@ -4,6 +4,8 @@ except ImportError:
pass
try:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtCore import QObject
except ImportError:
pass
@ -12,23 +14,23 @@ from lightweight_charts.js import LWC
class WxChart(LWC):
def __init__(self, parent, volume_enabled=True):
super().__init__(volume_enabled)
try:
self.webview = wx.html2.WebView.New(parent)
self.webview: wx.html2.WebView = wx.html2.WebView.New(parent)
except NameError:
raise ModuleNotFoundError('wx.html2 was not found, and must be installed to use WxChart.')
super().__init__(volume_enabled)
self.webview.AddScriptMessageHandler('wx_msg')
self._click_func_code('window.wx_msg.postMessage(data)')
self.webview.Bind(wx.html2.EVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, lambda e: self._js_api.onClick(eval(e.GetString())))
self.webview.Bind(wx.html2.EVT_WEBVIEW_LOADED, self._on_js_load)
self.webview.SetPage(self._html, '')
self.second_load = False
def run_script(self, script): self.webview.RunScript(script)
def _on_js_load(self, e):
if not self.second_load:
self.second_load = True
return
self.loaded = True
for func, args, kwargs in self.js_queue:
getattr(super(), func)(*args, **kwargs)
@ -38,12 +40,13 @@ class WxChart(LWC):
class QtChart(LWC):
def __init__(self, widget=None, volume_enabled=True):
super().__init__(volume_enabled)
try:
self.webview = QWebEngineView(widget)
except NameError:
raise ModuleNotFoundError('QWebEngineView was not found, and must be installed to use QtChart.')
super().__init__(volume_enabled)
self.webview.loadFinished.connect(self._on_js_load)
self.webview.page().setHtml(self._html)