6.6 KiB
API
webview.create_window
webview.create_window(title, url='', html='', js_api=None, width=800, height=600, resizable=True,\
fullscreen=False, min_size=(200, 100), confirm_close=False, \
background_color='#FFF', text_select=False)
Create a new pywebview window and returns its instance. Window is not shown until the GUI loop is started. If the function is invoked during the GUI loop, the window is displayed immediately.
title- Window titleurl- URL to load. If the URL does not have a protocol prefix, it is resolved as a path relative to the application entry point.html- HTML code to load. If both URL and HTML are specified, HTML takes precedence.js_api- Expose ajs_apiclass object to the DOM of the currentpywebviewwindow. Callable functions ofjs_apican be executed using Javascript page viawindow.pywebview.apiobject. Custom functions accept a single parameter, either a primitive type or an object. Object types are converted between Javascript and Python. Functions are executed in separate threads and are not thread-safe.window.pywebviewis not guaranteed to be available onwindow.onloadand its access must be deferred.width- Window width. Default is 800px.height- Window height. Default is 600px.resizable- Whether window can be resized. Default is Truefullscreen- Start in fullscreen mode. Default is Falseframeless- Create a frameless easy-draggable window. Default is False.min_size- a (width, height) tuple that specifies a minimum window size. Default is 200x100confirm_close- Whether to display a window close confirmation dialog. Default is Falsebackground_color- Background color of the window displayed before WebView is loaded. Specified as a hex color. Default is white.text_select- Enables document text selection. Default is False. To control text selection on per element basis, use user-select CSS property.
webview.start
webview.start(func=None, args=None, localization={}, http_server=False, gui=None, debug=False)
Start a GUI loop and display previously created windows. This function must be called from a main thread.
func- function to invoke upon starting the GUI loop.args- function arguments. Can be either a single value or a tuple of values.localization- a dictionary with localized strings. Default strings and their keys are defined in localization.pyhttp_server- enable built-in HTTP server. If enabled, local files will be served using a local HTTP server on a random port. For each window, a separate HTTP server is spawned. This option is ignored for non-local URLs.gui- force a specific GUI. Allowed values arecef,qtorgtkdepending on a platform. See Renderer for details.debug- enable debug mode. See Debugging for details.
Examples
webview.token
webview.token
A CSRF token property unique to the session. The same token is exposed as window.pywebview.token. See Security for usage details.
Window object
These functions are part of the window object returned by create_window
create_file_dialog
create_file_dialog(dialog_type=OPEN_DIALOG, directory='', allow_multiple=False, save_filename='', file_types=())`
Create an open file (webview.OPEN_DIALOG), open folder (webview.FOLDER_DIALOG) or save file (webview.SAVE_DIALOG) dialog.
Return a tuple of selected files, None if cancelled.
allow_multiple=Trueenables multiple selection.directoryInitial directory.save_filenameDefault filename for save file dialog.file_typesA tuple of supported file type strings in the open file dialog. A file type string must follow this format"Description (*.ext1;*.ext2...)".
If the argument is not specified, then the "All files (*.*)" mask is used by default. The 'All files' string can be changed in the localization dictionary.
Examples
destroy
destroy()
Destroy the window.
evaluate_js
evaluate_js(script)
Execute Javascript code. The last evaluated expression is returned. Javascript types are converted to Python types, eg. JS objects to dicts, arrays to lists, undefined to None. Note that due implementation limitations the string 'null' will be evaluated to None.
You must escape \n and \r among other escape sequences if they present in Javascript code. Otherwise they get parsed by Python. r'strings' is a recommended way to load Javascript. For GTK WebKit2 versions older than 2.22, there is a limit of about ~900 characters for a value returned by evaluate_js.
get_current_url
get_current_url()
Return the current URL. None if no url is loaded.
get_elements
get_elements(selector)
Return the serialized DOM element by its selector. None if no element matches. For GTK you must have WebKit2 2.22 or greater to use this function.
load_css
load_css(css)
Load CSS as a string.
load_html
load_html(content, base_uri=base_uri())
Load HTML code. Base URL for resolving relative URLs is set to the directory the program is launched from. Note that you cannot use hashbang anchors when HTML is loaded this way.
load_url
load_url(url)
Load a new URL.
set_title
set_title(title)
Change the title of the window.
toggle_fullscreen
toggle_fullscreen()
Toggle fullscreen mode on the active monitor.
Events
Window object has these lifecycle events. To subscribe to an event, use the += syntax, e.g. window.loaded += func. The func will be invoked, when event is fired. Duplicate subscriptions are ignored and function is invoked only once for duplicate subscribers. To unsubscribe window.loaded -= func.
shown
Event that is fired when pywebview window is shown.
loaded
Event that is fired when DOM is ready.