Skip to content

Commit

Permalink
Control pywebview show/hide via pystray
Browse files Browse the repository at this point in the history
  • Loading branch information
TechNoteGit committed Sep 12, 2021
1 parent 540c4c9 commit 3a35562
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions basic.py
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()
Binary file added res/tray_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3a35562

Please sign in to comment.