Skip to content Skip to sidebar Skip to footer

How To Close Pywebview Window From Javascript Using Pywebview Api

I was building an application in pywebview with html and flask i decided to make use of fullscreen mode and make custom nice looking buttons for minimize and exit i.e(window.open('

Solution 1:

You just need to add a class called Api in your Python code, like it is explained in the article you linked. In your case, that class only needs to include the constructor and a method like this:

def quit(self):
    self._window.destroy()

(and any other methods you need to execute from python), and then invoke it from javascript with pywebview.api.quit().

UPDATE: Forgot to mention an important detail. When you create your window, store it in a variable, like so:

  api = Api()

  window = webview.create_window(
    ...
    js_api=api,
    ...
  )
  api.set_window(window)

So your Api class would look something like this:

class Api:

  def __init__(self):
    self._window = None

  def set_window(self, window):
    self._window = window

  def quit(self):
    self._window.destroy()

Post a Comment for "How To Close Pywebview Window From Javascript Using Pywebview Api"