Skip to content

Commit 72b8668

Browse files
committed
Add an ability to destroy a window programmatically
1 parent 1ae089e commit 72b8668

7 files changed

Lines changed: 101 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ For more elaborated usage, refer to the examples in the examples folder
5353
- `webview.load_url(url)`
5454
Load a new URL into a previously created WebView window. This function must be invoked after WebView windows is created with create_window(). Otherwise an exception is thrown.
5555

56-
- `webview.create_file_dialog(dialog_type=OPEN_DIALOG, allow_multiple=False)`
57-
Create an open file (`webview.OPEN_DIALOG`) or open folder (`webview.FOLDER_DIALOG`) dialog. `allow_multiple=True` enables multiple selection. Return a tuple of selected files, None if cancelled
56+
- `webview.create_file_dialog(dialog_type=OPEN_DIALOG, directory='', allow_multiple=False, save_filename='')`
57+
Create an open file (`webview.OPEN_DIALOG`), open folder (`webview.FOLDER_DIALOG`) or save file (`webview.SAVE_DIALOG`) dialog. `allow_multiple=True` enables multiple selection. `directory` Initial directory. `save_filename` Default filename for save file dialog.
58+
Return a tuple of selected files, None if cancelled
59+
60+
- `webview.destroy_window()`
61+
Destroy a webview window
5862

5963

6064
# What web renderer is used?
@@ -84,6 +88,17 @@ Use py2app on OS X and py2exe on Windows. For reference setup.py files, look in
8488

8589
# Changelog
8690

91+
92+
## 1.0
93+
94+
- `New` [All] Add an ability to programmatically destroy a webview window
95+
- `Fix` [Windows] Fullscreen mode
96+
- `Fix` [Windows] Change setup.py to use pypiwin32 #22
97+
- `Fix` [Windows] Relative import of win32_gen fixed on Python 3 #20. Thanks to @yoavram for the contribution
98+
- `Fix` [Windows] FileNotFound exception on Windows 2003. Thanks to @jicho for the contribution
99+
- `Fix` [OSX] Non-SSL URLs are allowed by default on El Capitan. Thanks to @cr0hn for the contribution
100+
101+
87102
## 0.9
88103

89104
Released 27/11/2015

‎examples/destroy_window.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import webview
2+
import threading
3+
4+
"""
5+
This example demonstrates how a webview window is created and destroyed programmatically after 5 seconds
6+
"""
7+
#import os
8+
#os.environ["USE_GTK"] = "true"
9+
10+
def destroy():
11+
import time
12+
time.sleep(5)
13+
print("Destroying window..")
14+
webview.destroy_window()
15+
16+
17+
if __name__ == '__main__':
18+
t = threading.Thread(target=destroy)
19+
t.start()
20+
webview.create_window("Simple browser", "http://www.google.com")
21+
print("Window is destroyed")
22+

‎setup.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
author_email = "roman@flowrl.com",
1515
description = ("A cross-platform lightweight native wrapper around a web view component"),
1616
url = "http://github.com/r0x0r/pywebview",
17-
download_url = 'https://github.com/r0x0r/pywebview/archive/0.9.tar.gz',
18-
keywords = ['gui', 'webkit', 'html', 'web' ],
17+
download_url = 'https://github.com/r0x0r/pywebview/archive/1.0.tar.gz',
18+
keywords = ['gui', 'webkit', 'html', 'web'],
1919
install_requires = install_requires,
20-
version='0.9',
20+
version='1.0',
2121
packages=['webview',],
2222
license='New BSD license',
2323
classifiers=[

‎webview/__init__.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@
5454
raise Exception("Unsupported platform. Only Windows, Linux and OS X are supported.")
5555

5656

57+
5758
def create_file_dialog(dialog_type=OPEN_DIALOG, directory='', allow_multiple=False, save_filename=''):
59+
"""
60+
Create a file dialog
61+
:param dialog_type: Dialog type: open file (OPEN_DIALOG), save file (SAVE_DIALOG), open folder (OPEN_FOLDER). Default
62+
is open file.
63+
:param directory: Initial directory
64+
:param allow_multiple: Allow multiple selection. Default is false.
65+
:param save_filename: Default filename for save file dialog.
66+
:return:
67+
"""
5868
if not os.path.exists(directory):
5969
directory = ''
6070

@@ -84,6 +94,12 @@ def create_window(title, url, width=800, height=600, resizable=True, fullscreen=
8494
gui.create_window(_make_unicode(title), _transform_url(url), width, height, resizable, fullscreen, min_size)
8595

8696

97+
def destroy_window():
98+
"""
99+
Destroy a web view window
100+
"""
101+
gui.destroy_window()
102+
87103
def _make_unicode(string):
88104
"""
89105
Python 2 and 3 compatibility function that converts a string to Unicode. In case of Unicode, the string is returned

‎webview/gtk.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def show(self):
6262
gtk.main()
6363
Gdk.threads_leave()
6464

65+
def destroy(self):
66+
Gdk.threads_enter()
67+
self.window.destroy()
68+
Gdk.threads_leave()
69+
6570
def create_file_dialog(self, dialog_type, directory, allow_multiple, save_filename):
6671
Gdk.threads_enter()
6772

@@ -112,6 +117,13 @@ def create_window(title, url, width, height, resizable, fullscreen, min_size):
112117
browser.show()
113118

114119

120+
def destroy_window():
121+
if BrowserView.instance is not None:
122+
BrowserView.instance.destroy()
123+
else:
124+
raise Exception("Create a web view window first, before invoking this function")
125+
126+
115127
def load_url(url):
116128
if BrowserView.instance is not None:
117129
BrowserView.instance.load_url(url)

‎webview/qt.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class BrowserView(QMainWindow):
5151
instance = None
5252
url_trigger = QtCore.pyqtSignal(str)
5353
dialog_trigger = QtCore.pyqtSignal(int, str, bool, str)
54+
destroy_trigger = QtCore.pyqtSignal()
5455

5556
def __init__(self, title, url, width, height, resizable, fullscreen, min_size):
5657
super(BrowserView, self).__init__()
@@ -76,6 +77,7 @@ def __init__(self, title, url, width, height, resizable, fullscreen, min_size):
7677
self.setCentralWidget(self.view)
7778
self.url_trigger.connect(self._handle_load_url)
7879
self.dialog_trigger.connect(self._handle_file_dialog)
80+
self.destroy_trigger.connect(self._handle_destroy_window)
7981

8082
self.move(QApplication.desktop().availableGeometry().center() - self.rect().center())
8183
self.activateWindow()
@@ -100,6 +102,9 @@ def _handle_file_dialog(self, dialog_type, directory, allow_multiple, save_filen
100102
def _handle_load_url(self, url):
101103
self.view.setUrl(QtCore.QUrl(url))
102104

105+
def _handle_destroy_window(self):
106+
self.close()
107+
103108
def load_url(self, url):
104109
self.url_trigger.emit(url)
105110

@@ -119,6 +124,10 @@ def create_file_dialog(self, dialog_type, directory, allow_multiple, save_filena
119124
else:
120125
return None
121126

127+
def destroy_(self):
128+
self.destroy_trigger.emit()
129+
130+
122131

123132
def create_window(title, url, width, height, resizable, fullscreen, min_size):
124133
"""
@@ -135,7 +144,7 @@ def create_window(title, url, width, height, resizable, fullscreen, min_size):
135144

136145
browser = BrowserView(title, url, width, height, resizable, fullscreen, min_size)
137146
browser.show()
138-
sys.exit(app.exec_())
147+
app.exec_()
139148

140149

141150
def load_url(url):
@@ -145,6 +154,13 @@ def load_url(url):
145154
raise Exception("Create a web view window first, before invoking this function")
146155

147156

157+
def destroy_window():
158+
if BrowserView.instance is not None:
159+
BrowserView.instance.destroy_()
160+
else:
161+
raise Exception("Create a web view window first, before invoking this function")
162+
163+
148164
def create_file_dialog(dialog_type, directory, allow_multiple, save_filename):
149165
if BrowserView.instance is not None:
150166
return BrowserView.instance.create_file_dialog(dialog_type, directory, allow_multiple, save_filename)

‎webview/win32.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ def show(self):
180180
# Pump messages
181181
win32gui.PumpMessages()
182182

183+
def destroy(self):
184+
win32gui.SendMessage(self.hwnd, win32con.WM_DESTROY)
185+
183186
def load_url(self, url):
184187
self.url = url
185188
self.browser.Navigate2(url)
@@ -229,10 +232,13 @@ def create_file_dialog(self, dialog_type, directory, allow_multiple, save_filena
229232
except:
230233
return None
231234

232-
def _on_destroy(self, hwnd, message, wparam, lparam):
235+
def _destroy(self):
233236
del self.browser
234237
win32gui.PostQuitMessage(0)
235238

239+
def _on_destroy(self, hwnd, message, wparam, lparam):
240+
self._destroy()
241+
236242
return True
237243

238244
def _on_resize(self, hwnd, message, wparam, lparam):
@@ -282,6 +288,13 @@ def load_url(url):
282288
raise Exception("Create a web view window first, before invoking this function")
283289

284290

291+
def destroy_window():
292+
if BrowserView.instance is not None:
293+
BrowserView.instance.destroy()
294+
else:
295+
raise Exception("Create a web view window first, before invoking this function")
296+
297+
285298
def _set_ie_mode():
286299
"""
287300
By default hosted IE control emulates IE7 regardless which version of IE is installed. To fix this, a proper value

0 commit comments

Comments
 (0)