0

I want to change a row of layout within a frame by changing event from combobox. And change back when first value from combobox is triggered.

When the window starts, it looks like below. enter image description here

When user change value in combobox from Class A to Class B.

enter image description here

Window updates with new frame showing Class B.

enter image description here

window[key-name].update(visible=True) does't work with frame?

The Code:

import PySimpleGUI as sg

sg.theme("Reddit")

layout = [ [sg.Frame(
          layout=[
[sg.Text("ID:",key='-ID-',size=(10, 1)),
sg.InputText(key='-section-id-',size=(10, 1), tooltip=" Define an id")
],
[
    sg.Frame(
        layout=[
            [sg.Text("Student:",key='-student-',size=(10, 1)),
             sg.InputText(default_text="25",key='-Class1-',size=(5, 1)),
             sg.InputText(default_text="25",key='-Class2-',size=(5, 1)),
             sg.Combo(['Yes', 'No'],key='-Yes&No-', default_value='Yes'),
             sg.Combo(['A','B'],key='-A-B-',default_value='A')],
        ],
        title="Student I",
        relief=sg.RELIEF_GROOVE,
    )
],
[
    sg.Frame(
        layout=[
             [sg.Text("Student in A:",key='-IN-A-',size=(10, 1)),
             sg.InputText(default_text="20",key='-MALE-',size=(5, 1), tooltip=" Number of males in Class A"),
             sg.InputText(default_text="30",key='-FEMALE-',size=(5, 1), tooltip=" Number of females in Class A"),
             sg.Combo(['A1','A2'],key='-A1-A2-',default_value='A1'),
             sg.Combo(['Class A','Class B'],key='-CLASS-A-CLASS-B-',default_value='Class A',size=(8,1),
             enable_events=True)],
        ],
        title="Main class",
        relief=sg.RELIEF_GROOVE,
    )
],
# [
#     sg.Frame(
#         layout=[
#             [sg.Text("Student in B:", key='-IN-A-', size=(10, 1)),
#              sg.InputText(default_text="10", key='-FEMALE-', size=(5, 1), tooltip=" Number of females in Class B"),
#              sg.InputText(default_text="30", key='-FEMALE-', size=(5, 1), tooltip=" Number of males in Class B"),
#              sg.Combo(['B1', 'B2'], key='-B1-B2-', default_value='B1'),
#              sg.Combo(['Class A', 'Class B'], key='-CLASS-A-CLASS-B-', default_value='Class B', size=(8, 1),
#                       enable_events=True)],
#         ],
#         title="Main class",
#         relief=sg.RELIEF_GROOVE,
#     )
# ]

          ],
          title="Add an ID",
          relief=sg.RELIEF_GROOVE,
             )
              ],
[sg.Push(),sg.Button('Add ID',key='-Add-ID-',size=(10,1),font=("",8,"bold")),
 sg.Button('Cancel',key='-Cancel-',size=(5,1),font=("",8,"bold"))]
 ]

window = sg.Window("Add an ID", layout, resizable=True,  keep_on_top=True)

while True:
   event, values = window.read()
   if event == sg.WINDOW_CLOSED or event == '-Cancel-':
      sys.exit()
   if event == '-CLASS-A-CLASS-B-' and values['-CLASS-A-CLASS-B-'] == 'Class B':
      window.refresh()
      print('Tested ok!'+' '+values['-CLASS-A-CLASS-B-'])
window.close()

1 Answer 1

1

Following simple and short Code demo how to switching between Frames by Combo event.

import PySimpleGUI as sg

layout = [
    [sg.Combo(["Class A", "Class B"], default_value="Class A", enable_events=True, key='COMBO')],
    [sg.Frame("Class A", [[sg.Text("This is the Class A Frame")]], key='Class A'),
     sg.Frame("Class B", [[sg.Text("This is the Class B Frame")]], key='Class B', visible=False)],
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == "COMBO":
        selection = values[event]
        if selection in ("Class A", "Class B"):
            window["Class A"].update(visible=(selection=="Class A"))
            window["Class B"].update(visible=(selection=="Class B"))

window.close()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Great @Jason. Simple and short demo and educational :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.