r0x0r / pywebview

Build GUI for your Python program with JavaScript, HTML, and CSS
https://pywebview.flowrl.com
BSD 3-Clause "New" or "Revised" License
4.47k stars 529 forks source link

Pystray Icon #1423

Open Jetrom17 opened 4 days ago

Jetrom17 commented 4 days ago

Linux: ZorinOS.

Following the documentation, that is, installation and dependencies suggested by pywebview. Pywebview is not starting with example code in the documentation itself.

import multiprocessing
import sys

from PIL import Image
from pystray import Icon, Menu, MenuItem

import webview

if sys.platform == 'darwin':
    ctx = multiprocessing.get_context('spawn')
    Process = ctx.Process
    Queue = ctx.Queue
else:
    Process = multiprocessing.Process
    Queue = multiprocessing.Queue

webview_process = None

def run_webview():
    window = webview.create_window('Webview', 'https://pywebview.flowrl.com/hello')
    webview.start()

if __name__ == '__main__':

    def start_webview_process():
        global webview_process
        webview_process = Process(target=run_webview)
        webview_process.start()

    def on_open(icon, item):
        global webview_process
        if not webview_process.is_alive():
            start_webview_process()

    def on_exit(icon, item):
        icon.stop()

    start_webview_process()

    image = Image.open('logo/logo.png')
    menu = Menu(MenuItem('Open', on_open), MenuItem('Exit', on_exit))
    icon = Icon('Pystray', image, menu=menu)
    icon.run()

    webview_process.terminate()

Font: https://pywebview.flowrl.com/examples/pystray_icon.html

Debug:

python3 test.py 
[pywebview] Using GTK

(test.py:45851): GVFS-WARNING **: 11:17:26.823: Error creating proxy: Erro ao chamar StartServiceByName para org.gtk.vfs.Daemon: O tempo limite foi alcançado (g-io-error-quark, 24)

Gdk-Message: 11:17:26.962: Error 22 (Argumento inválido) dispatching to Wayland display.
EGLDisplay Initialization failed: EGL_NOT_INITIALIZED
jeiel@jeiel-pc:~/test/squarecloud$ 
** (test.py:45851): WARNING **: 11:17:27.126: Disabled hardware acceleration because GTK failed to initialize GL: Nenhuma implementação GL está disponível.
Could not determine the accessibility bus address
Gdk-Message: 11:17:57.471: Error 32 (Pipe quebrado) dispatching to Wayland display.
r0x0r commented 3 days ago

I believe the pystray example has never been tested on Linux. Contributions are welcomed.