question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do destroy window by calling a Python function via the JS API?

See original GitHub issue

Specification

  • pywebview version: 3.3.5
  • platform / version: Windows 10

Description

import webview


html = """
<!DOCTYPE html>
<html>
<head lang="en">
</head>
<body>
<h1>JS API Example</h1>
<button onclick="close()">close</button>
<script>
  function close() {
      pywebview.api.close();
  }
</script>
</body>
</html>
"""


class Api:

  def close(self):
      window.destroy()


window = webview.create_window('test',
                             html=html,
                             easy_drag=False,
                             js_api=Api,
                             frameless=True)
webview.start()

Practicalities

  • YES I am willing to work on this issue myself.

  • NO I am prepared to support this issue financially.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dtmilanocommented, Nov 28, 2020

Try _close() as close() is already defined

<!DOCTYPE html>
<html>
<head lang="en">
</head>
<body>
<h1>JS API Example</h1>
<button onclick="_close()">close</button>
<script>
    function _close() {
        pywebview.api.close();
    }
</script>
</body>
</html>
0reactions
r0x0rcommented, Dec 1, 2020

close is a global js function. this has nothing to do with pywebview.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to return a "close window" command from a GET call?
You could change your Python lambda script to return HTML and execute the window.close() javascript function.
Read more >
Window.close() - Web APIs - MDN Web Docs - Mozilla
The Window.close() method closes the current window, or the window on which it was called.
Read more >
Window close() Method - W3Schools
Use open() to open a window and close() to close the window: let myWindow; function openWin() { myWindow = window.open("", "myWindow", "width=200, ...
Read more >
How to close window using JavaScript which is opened by the ...
Step 2: Close this open window using the close() method: The window.close() method closes the window on which it is called. The window...
Read more >
A First Look at PyScript: Python in the Web Browser
You'll leverage browser APIs and JavaScript libraries to build rich, ... you can call Pyodide's loadPackage() function using its Python ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found