-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Control pywebview show/hide via pystray
- Loading branch information
1 parent
540c4c9
commit 3a35562
Showing
2 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,50 @@ | ||
import os | ||
import webview | ||
import pystray | ||
import time | ||
import multiprocessing | ||
from pystray import MenuItem | ||
from pystray import Menu | ||
from multiprocessing import Process, Pipe | ||
from PIL import Image | ||
|
||
|
||
def webview_subprocess(child_pipe): | ||
window = webview.create_window('TechNote', 'https://technote.kr') | ||
webview.start(cmd_recv, [window, child_pipe], gui='cef', debug=True) | ||
webview.start(cmd_recv, [child_pipe], gui='cef', debug=True) | ||
|
||
|
||
def cmd_recv(window, child_pipe): | ||
def cmd_recv(child_pipe): | ||
while True: | ||
cmd = child_pipe.recv() | ||
# To Do - cmd handler | ||
if cmd == 'show': | ||
webview.windows[0].show() | ||
elif cmd == 'hide': | ||
webview.windows[0].hide() | ||
|
||
|
||
def send_cmd_to_window(parent_pipe, cmd): | ||
parent_pipe.send(cmd) | ||
|
||
|
||
def quit_window(subprocess_handler): | ||
icon.stop() | ||
subprocess_handler.terminate() | ||
|
||
|
||
if __name__ == '__main__': | ||
parent_pipe, child_pipe = Pipe() | ||
|
||
subprocess_handler = Process(target=webview_subprocess, args=(child_pipe,)) | ||
subprocess_handler.start() | ||
|
||
# Using window tray | ||
base_path = os.path.dirname(os.path.abspath(__file__)) | ||
image_path = Image.open(base_path + '/res/tray_icon.png') | ||
menu = Menu(MenuItem('Hide', lambda: send_cmd_to_window(parent_pipe, 'hide')), | ||
MenuItem('Show', lambda: send_cmd_to_window(parent_pipe, 'show')), | ||
MenuItem('Quit', lambda: quit_window(subprocess_handler))) | ||
icon = pystray.Icon('pyWebView_Sample', image_path, 'pyWebView', menu) | ||
icon.run() | ||
|
||
subprocess_handler.join() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.