PySimpleGUI / psgtray

A System Tray Icon that works with the PySimpleGUI tkinter port. Uses pystray to supply the system tray. Works well under Windows.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading clicked value without using window object

mddanish00 opened this issue · comments

Hi!

What I want is like a tray.read() that exists in PySimpleGUIQt.

My app did use Window object in some parts of my app, but most of the time, only the system tray will remain. So, the option of using the window object is not available for me. I am previously on PySimpleGUIQt, so I can do this. I wondering if that tray.read() can only be implemented in this library too.

Thanks!

Hiya....

I've got 2 programs that I use all day every day, pingmote & pysimplehotkey. Both are in repos here.

My memory is really foggy on this project, but it does appear they both have windows, but I never really use the window for the hotkey.

I would suggest, assuming a window is required, that you constantly keep the window hidden. If the window is brought into view, hide it. Or, you can set the alpha channel on the window to 0 when you create it.

I think it's going to be a case of "gotta have one, but don't have to see it" given the architecture I see.

Maybe something like this would work?

import PySimpleGUI as sg
from psgtray import SystemTray


"""
    A System Tray Icon courtesy of pystray and your friends at PySimpleGUI
    
    Import the SystemTray object with this line of code:
    from psgtray import SystemTray

    Key for the system tray icon is: 
        tray = SystemTray()
        tray.key
        
    values[key] contains the menu item chosen.
    
    One trick employed here is to change the window's event to be the event from the System Tray.
    
    
    Copyright PySimpleGUI 2021
"""

def main():

    menu = ['', [ '---', '!Disabled Item', 'Change Icon', ['Happy', 'Sad', 'Plain'], 'Exit']]
    tooltip = 'Tooltip'

    layout = [[sg.T('Empty Window', key='-T-')]]

    window = sg.Window('Window Title', layout, finalize=True, enable_close_attempted_event=True, alpha_channel=0)
    window.hide()

    tray = SystemTray(menu, single_click_events=False, window=window, tooltip=tooltip, icon=sg.DEFAULT_BASE64_ICON, key='-TRAY-')
    tray.show_message('System Tray', 'System Tray Icon Started!')
    print(sg.get_versions())
    while True:
        event, values = window.read()
        # IMPORTANT step. It's not required, but convenient. Set event to value from tray
        # if it's a tray event, change the event variable to be whatever the tray sent
        if event == tray.key:
            event = values[event]       # use the System Tray's event as if was from the window

        if event in (sg.WIN_CLOSED, 'Exit'):
            break

        tray.show_message(title=event, message=values)

        if event == 'Happy':
            tray.change_icon(sg.EMOJI_BASE64_HAPPY_JOY)
        elif event == 'Sad':
            tray.change_icon(sg.EMOJI_BASE64_FRUSTRATED)
        elif event == 'Plain':
            tray.change_icon(sg.DEFAULT_BASE64_ICON)
        elif event == 'Hide Icon':
            tray.hide_icon()
        elif event == 'Show Icon':
            tray.show_icon()
        elif event == 'Change Tooltip':
            tray.set_tooltip(values['-IN-'])

    tray.close()            # optional but without a close, the icon may "linger" until moused over
    window.close()

if __name__ == '__main__':
    main()

I've added this code above to the repo as another demo program. The same Demo Program was added to the primary PySimpleGUI repo as well.

G'luck in what you're making. If you have a moment, post a screenshot in Issue # 10 in the PySimpleGUI Repo.

Sorry for the late response. I tried on my application and it works. Thanks for all your help!

No need for any kind of "sorry", at all.

Thanks for all your help!

image

You are quite welcome! I'm really happy to hear your application is working. Very nice!