I. Installing the PyWebView Library

1.1 Installation

Run the following command in the terminal.

1
pip install pywebview

1.2 Troubleshooting

The issue arises because PyWebView relies on the pythonnet dependency library, and incorrect installation may lead to runtime errors.

  1. Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet下载对应的pythonnet的whl文件。
  2. Use pip uninstall pythonnet to uninstall the previously installed pythonnet.
  3. Then, reinstall pythonnet using the pip install xxx.whl command.

II. Creating a Window

webview.create_window
1
2
3
4
5
webview.create_window(title, url='', html='', js_api=None, width=800, height=600, \
x=None, y=None, resizable=True, fullscreen=False, \
min_size=(200, 100), hidden=False, frameless=False, \
minimized=False, on_top=False, confirm_close=False, \
background_color='#FFF')

Creates a new PyWebView window and returns its instance. The window will not be displayed until the GUI loop starts.

  • title Window title

  • url The URL to load. If the URL does not have a protocol prefix, it will be resolved as a path relative to the application entry point. Alternatively, you can pass a WSGI server object to start a local web server.

  • html The HTML code to load. If both URL and HTML are specified, HTML takes precedence.

  • js_api Exposes a Python object to the DOM of the current PyWebView window. Methods of the js_api object can be executed from JavaScript by calling

    1
    window.pywebview.api.<methodname>(<parameters>)

    Note that calling a JavaScript function will receive a return value containing a Python function. Only basic Python objects (e.g., int, str, dict, etc.) can be returned to JavaScript.

  • width Window width. Default value is 800px.

  • height Window height. Default value is 600px.

  • x Window x-coordinate. Default is centered.

  • y Window y-coordinate. Default is centered.

  • resizable Whether the window can be resized. Default value is True.

  • fullscreen Start in fullscreen mode. Default is False.

  • min_size A tuple specifying the minimum window size (width, height). Default value is 200x100.

  • hidden Creates a hidden window by default. Default is False.

  • frameless Creates a window without a frame. Default value is False.

  • minimized Starts in minimized mode.

  • on_top Sets the window to always be on top of other windows. Default value is False.

  • confirm_close Whether to show a confirmation dialog when closing the window. Default is False.

  • background_color The background color of the window before loading WebView. Specified as a hexadecimal color. Default value is white.

  • transparent Creates a transparent window. Not supported on Windows. Default value is False.

III. Displaying the Window

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

Starts the GUI loop and displays the previously created window. This function must be called from the main thread.

  • func The function to be called when starting the GUI loop.
  • args Function arguments. Can be a single value or a tuple of values.
  • localization A dictionary with localization strings. Default strings and their keys are defined in localization.py.
  • gui Forces the use of a specific GUI. Allowed values are cef, qt, or gtk, depending on the platform.
  • debug Enables debug mode.
  • http_server Enables the built-in HTTP server. If enabled, local files will be served on a random port using the local HTTP server. A separate HTTP server will be generated for each window. This option will be ignored for non-local URLs.