使用参数在 pywebview 中加载 html

Load html in pywebview with arguments

我想在 pywebview (desmos API) 中加载 html 代码,但我必须在函数 load_html 中包含参数,但当 运行宁。我有以下代码:

import webview

def load_html(window,userExpr,vars):
    htmlCode = 'html code' + userExpr + 'more html code' + vars + 'even more html code'
    window.load_html(htmlCode)

userExpr = "y=x^2"
vars = [-10,10]
window = webview.create_window("Graph", width=1210, height=820)
webview.start(load_html,window,userExpr,vars)

我希望它使用包含的参数加载 html 代码,但是当我 运行 程序时出现错误:

Traceback (most recent call last):
  File "C:/path/test.py", line 10, in <module>
    webview.start(load_html,window,vars)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\webview\__init__.py", line 71, in start
    original_localization.update(localization)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

如何解决这个问题?

start() 预计只有一个参数。

对于许多参数,使用列表或元组 - [window, userExpr, vars]

import webview

def load_html(window, userExpr, vars):
    print(window, userExpr, vars)
    htmlCode = 'html code' + userExpr + 'more html code' + str(vars) + 'even more html code'
    window.load_html(htmlCode)

userExpr = "y=x^2"
vars = [-10,10]
window = webview.create_window("Graph", width=1210, height=820)
webview.start(load_html, [window, userExpr, vars] )

其他地方有不同的选择 - 请参阅 documentation for start()

webview.start(func=None, args=None, localization={}, http_server=False, gui=None, debug=False)