pysimplegui to run multiple windows

Posted by jarcoal on Sat, 15 Jan 2022 19:20:23 +0100

Run multiple windows

This is where PySimpleGUI continues to be simple, but the problem space has just entered the "complex" field.

If you want to run multiple windows in an event loop, there are two ways to do this.

  1. When the second window is visible, the first window does not remain active
  2. The first window remains active while the second window is visible

You will be in GitHub( http://www.PySimpleGUI.com )Two design considerations were found in the 2 demo programs in the Demo Program area of

It is important that you use the "new" layout every time you create a new window. You cannot reuse the layout from the previous window. As a result, you will see that the layout of window 2 is defined within a larger event loop.

If you have a window layout for use with a window and you have closed the window, you cannot use specific elements in the window. Layout variables must be recreated each time a new window is created. Read that sentence again... Layout every time you create a new window, you must recreate the variable. This means that you should have one The beginning statement layout =. I'm sorry to be stuck at this point, but many people seem to have trouble following this simple instruction.

Golden rule of window layout

You should not reuse the Windows layout Forever!

Or more specifically

If you are calling Window, you should define your Window layout Window in the statement before calling.

Multi window demo

There are several demo programs that can help you run multiple windows. Please download these programs and follow the examples they created for you.

Here are some code patterns you'll find when you look at the demo.

Multi window design mode 1 - both windows are active

import PySimpleGUI as sg

# Design pattern 2 - First window remains active

layout = [[ sg.Text('Window 1'),],
          [sg.Input(do_not_clear=True)],
          [sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Button('Launch 2'), sg.Button('Exit')]]

win1 = sg.Window('Window 1', layout)

win2_active = False
while True:
    ev1, vals1 = win1.read(timeout=100)
    win1['-OUTPUT-'].update(vals1[0])
    if ev1 == sg.WIN_CLOSED or ev1 == 'Exit':
        break

     if not win2_active and ev1 == 'Launch 2':
        win2_active = True
        layout2 = [[sg.Text('Window 2')],
                   [sg.Button('Exit')]]

        win2 = sg.Window('Window 2', layout2)

    if win2_active:
        ev2, vals2 = win2.read(timeout=100)
        if ev2 == sg.WIN_CLOSED or ev2 == 'Exit':
            win2_active  = False
            win2.close()

Multi window design mode 2 - only 1 active window

import PySimpleGUIQt as sg

# Design pattern 1 - First window does not remain active

layout = [[ sg.Text('Window 1'),],
          [sg.Input(do_not_clear=True)],
          [sg.Text(size=(15,1),  key='-OUTPUT-')],
          [sg.Button('Launch 2')]]

win1 = sg.Window('Window 1', layout)
win2_active=False
while True:
    ev1, vals1 = win1.read(timeout=100)
    if ev1 == sg.WIN_CLOSED:
        break
    win1.FindElement('-OUTPUT-').update(vals1[0])

    if ev1 == 'Launch 2'  and not win2_active:
        win2_active = True
        win1.Hide()
        layout2 = [[sg.Text('Window 2')],       # note must create a layout from scratch every time. No reuse
                   [sg.Button('Exit')]]

        win2 = sg.Window('Window 2', layout2)
        while True:
            ev2, vals2 = win2.read()
            if ev2 == sg.WIN_CLOSED or ev2 == 'Exit':
                win2.close()
                win2_active = False
                win1.UnHide()
                break

Topics: Python