From 0f6b1c22ab5a7ecfd03ade66760baff4adc45cf7 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 30 Apr 2022 08:33:32 -0400 Subject: [PATCH] New System Tray Icon demo using psgtray - Tray only version (no window is ever shown). Renamed other psgtray demo program to a similar name --- ...=> Demo_System_Tray_Icon_Using_psgtray.py} | 0 ...Demo_System_Tray_Icon_psgtray_No_Window.py | 69 +++++++++++++++++++ 2 files changed, 69 insertions(+) rename DemoPrograms/{Demo_psgtray_Tray_Icon_Tkinter.py => Demo_System_Tray_Icon_Using_psgtray.py} (100%) create mode 100644 DemoPrograms/Demo_System_Tray_Icon_psgtray_No_Window.py diff --git a/DemoPrograms/Demo_psgtray_Tray_Icon_Tkinter.py b/DemoPrograms/Demo_System_Tray_Icon_Using_psgtray.py similarity index 100% rename from DemoPrograms/Demo_psgtray_Tray_Icon_Tkinter.py rename to DemoPrograms/Demo_System_Tray_Icon_Using_psgtray.py diff --git a/DemoPrograms/Demo_System_Tray_Icon_psgtray_No_Window.py b/DemoPrograms/Demo_System_Tray_Icon_psgtray_No_Window.py new file mode 100644 index 00000000..daa6ce14 --- /dev/null +++ b/DemoPrograms/Demo_System_Tray_Icon_psgtray_No_Window.py @@ -0,0 +1,69 @@ +import PySimpleGUI as sg +from psgtray import SystemTray + +""" + A System Tray Icon using pystray - No visible window version + + 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. + + This demo program keeps the Window hidden all the time so that it's a pure "System Tray" application. + Because the PySimpleGUI architecture implemented the tray icon using the psgtray package combined with the + overall window event loop, a Window object is still required. The point of this demo is to show that this + window does not need to ever appear to the user. + + Copyright PySimpleGUI 2022 +""" + + +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() \ No newline at end of file