Want to use tray and window of pysimpleguiqt at the same time

Viewed 91

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)
1 Answers

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)

Related