Skip to content Skip to sidebar Skip to footer

How To Display Different Layouts Based On Button Clicks In Pysimple Gui? (persistent Window Loop)

I am wondering if there is a way that I can manage different layouts based on button clicks in PySimple GUI. I am just starting off using this framework and I want to find the best

Solution 1:

You're actually very close.

Here is what I think you're looking for. What you need to do is add your layouts to Column elements. Then make all columns invisible except for the one you want visible.

This is a great idea.

import PySimpleGUI as sg

# ----------- Create the 3 layouts this Window will display -----------
layout1 = [[sg.Text('This is layout 1 - It is all Checkboxes')],
           *[[sg.CB(f'Checkbox {i}')] for i inrange(5)]]

layout2 = [[sg.Text('This is layout 2')],
           [sg.Input(key='-IN-')],
           [sg.Input(key='-IN2-')]]

layout3 = [[sg.Text('This is layout 3 - It is all Radio Buttons')],
           *[[sg.R(f'Radio {i}', 1)] for i inrange(8)]]

# ----------- Create actual layout using Columns and a row of Buttons
layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-'), sg.Column(layout3, visible=False, key='-COL3-')],
          [sg.Button('Cycle Layout'), sg.Button('1'), sg.Button('2'), sg.Button('3'), sg.Button('Exit')]]

window = sg.Window('Swapping the contents of a window', layout)

layout = 1# The currently visible layoutwhileTrue:
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        breakif event == 'Cycle Layout':
        window[f'-COL{layout}-'].update(visible=False)
        layout = layout + 1if layout < 3else1
        window[f'-COL{layout}-'].update(visible=True)
    elif event in'123':
        window[f'-COL{layout}-'].update(visible=False)
        layout = int(event)
        window[f'-COL{layout}-'].update(visible=True)
window.close()

[EDIT] A new Demo Program was added to the PySimpleGUI GitHub named "Demo_Column_Elem_Swap_Entire_Window.py". You can see the code and run it in your browser by visiting Trinket.

Solution 2:

Using Google I found that there is no method to replace one layout with another.

PySimpleGUI Issues: Updating form layouts #117,

You can only create all elements in one layout and some of them hide and show again:

Reddit r/PySimpleGUI: Is it possible to update the layout of a column / frame?


In tkinter there is popular method to use Frame to group widgets and later hide/show frames to change content in window. It seems PySimpleGUI has also Frame but I don't have example which would work as I expected.

Post a Comment for "How To Display Different Layouts Based On Button Clicks In Pysimple Gui? (persistent Window Loop)"