Created
July 16, 2019 20:05
-
-
Save hkucuk/7277b8a056cb8c30dba6a394bae8d096 to your computer and use it in GitHub Desktop.
PyWebView Open File Dialog Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webview | |
import base64 | |
def open_file_dialog(window): | |
file_types = ('Image Files (*.bmp;*.jpg;*.gif;*.png)', | |
'All files (*.*)') | |
path = window.create_file_dialog(webview.OPEN_DIALOG, | |
allow_multiple=False, | |
file_types=file_types) | |
with open(path[0], "rb") as image_file: | |
encoded_string = base64.b64encode(image_file.read()) | |
window.evaluate_js( | |
r""" | |
var img = document.createElement('img'); | |
img.src = "data:image/png;base64,%s"; | |
document.getElementById('image-container').appendChild(img); | |
"""% ( encoded_string.decode('ascii')) | |
) | |
if __name__ == '__main__': | |
html = """ | |
<html> | |
<body> | |
<h1>Selected Image</h1> | |
</body> | |
</html> | |
""" | |
window = webview.create_window('Open file dialog example', html=html) | |
webview.start(open_file_dialog, window, debug=True,gui='cef') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice