2

I'm making an app where I want to use window and tray at the same time, but the following code needs an event from window first, then it gets the event of the tray, I tried doing both of them separately with threading but no luck there,

Try to do Exit, I mean from tray, and doubleclick tray multiple times, you will see the problem

import PySimpleGUIQt as Sg

for_default = ['a', 'v', 's']

menu_def = ['', ['---', '&p1', ['x', 'y'], '&p2', 'E&xit']]
layout = [[Sg.Text('Default    First     Second')],
          [Sg.Listbox(for_default, enable_events=True, key='-DEFAULT-'),
           Sg.Listbox(for_default, enable_events=True, key='-FIRST-'),
           Sg.Listbox(for_default, enable_events=True, key='-SECOND-')]]

window = Sg.Window('Window Title', layout, finalize=True)
tray = Sg.SystemTray(menu=menu_def)
tray.ShowMessage('My Message', 'The tray icon is up and runnning!')
while True:
    event, values, = window.Read()
    menu_item = tray.Read()
    if menu_item == 'Exit':
        print(event)
        print(menu_item)
        window.hide()
    elif menu_item == Sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED:
        window.un_hide()

    elif event == '-DEFAULT-':
        print(event)
LoNE WoLvES
  • 198
  • 1
  • 9
  • You'll need to run a timeout on both reads and thus flip back and forth for the time being. Something like 500ms should be fine. Add timeout=500 to your window and tray read calls. – Mike from PSG Sep 01 '21 at 19:14

1 Answers1

1

Found a good way to do this, just put window inside tray

import PySimpleGUIQt as Sg

for_default = ['a', 'v', 's']

menu_def = ['', ['---', '&p1', ['x', 'y'], '&p2', 'E&xit']]

tray = Sg.SystemTray(menu=menu_def)
tray.ShowMessage('My Message', 'The tray icon is up and runnning!')
while True:
    menu_item = tray.Read()
    if menu_item == 'Exit':
        break
    elif menu_item == Sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED:
        layout = [[Sg.Text('Default    First     Second')],
                  [Sg.Listbox(for_default, enable_events=True, key='-DEFAULT-'),
                   Sg.Listbox(for_default, enable_events=True, key='-FIRST-'),
                   Sg.Listbox(for_default, enable_events=True, key='-SECOND-')]]

        window = Sg.Window('Window Title', layout, finalize=True)
        while True:
            event, values, = window.Read()
            if event == '-DEFAULT-':
                print(event)

LoNE WoLvES
  • 198
  • 1
  • 9