MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_System_Tray_Icon_Using_psgtray.py:25–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23"""
24
25def main():
26
27 menu = ['', ['Show Window', 'Hide Window', '---', '!Disabled Item', 'Change Icon', ['Happy', 'Sad', 'Plain'], 'Exit']]
28 tooltip = 'Tooltip'
29
30 layout = [[sg.Text('My PySimpleGUI Celebration Window - X will minimize to tray')],
31 [sg.T('Double clip icon to restore or right click and choose Show Window')],
32 [sg.T('Icon Tooltip:'), sg.Input(tooltip, key='-IN-', s=(20,1)), sg.B('Change Tooltip')],
33 [sg.Multiline(size=(60,10), reroute_stdout=False, reroute_cprint=True, write_only=True, key='-OUT-')],
34 [sg.Button('Go'), sg.B('Hide Icon'), sg.B('Show Icon'), sg.B('Hide Window'), sg.Button('Exit')]]
35
36 window = sg.Window('Window Title', layout, finalize=True, enable_close_attempted_event=True)
37
38
39 tray = SystemTray(menu, single_click_events=False, window=window, tooltip=tooltip, icon=sg.DEFAULT_BASE64_ICON)
40 tray.show_message('System Tray', 'System Tray Icon Started!')
41 sg.cprint(sg.get_versions())
42 while True:
43 event, values = window.read()
44
45 # IMPORTANT step. It's not required, but convenient. Set event to value from tray
46 # if it's a tray event, change the event variable to be whatever the tray sent
47 if event == tray.key:
48 sg.cprint(f'System Tray Event = ', values[event], c='white on red')
49 event = values[event] # use the System Tray's event as if was from the window
50
51 if event in (sg.WIN_CLOSED, 'Exit'):
52 break
53
54 sg.cprint(event, values)
55 tray.show_message(title=event, message=values)
56
57 if event in ('Show Window', sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED):
58 window.un_hide()
59 window.bring_to_front()
60 elif event in ('Hide Window', sg.WIN_CLOSE_ATTEMPTED_EVENT):
61 window.hide()
62 tray.show_icon() # if hiding window, better make sure the icon is visible
63 # tray.notify('System Tray Item Chosen', f'You chose {event}')
64 elif event == 'Happy':
65 tray.change_icon(sg.EMOJI_BASE64_HAPPY_JOY)
66 elif event == 'Sad':
67 tray.change_icon(sg.EMOJI_BASE64_FRUSTRATED)
68 elif event == 'Plain':
69 tray.change_icon(sg.DEFAULT_BASE64_ICON)
70 elif event == 'Hide Icon':
71 tray.hide_icon()
72 elif event == 'Show Icon':
73 tray.show_icon()
74 elif event == 'Change Tooltip':
75 tray.set_tooltip(values['-IN-'])
76
77 tray.close() # optional but without a close, the icon may "linger" until moused over
78 window.close()
79
80if __name__ == '__main__':
81 main()

Calls 9

show_messageMethod · 0.95
readMethod · 0.95
un_hideMethod · 0.95
bring_to_frontMethod · 0.95
hideMethod · 0.95
closeMethod · 0.95
closeMethod · 0.95
SystemTrayClass · 0.85
set_tooltipMethod · 0.80

Tested by

no test coverage detected