PysimpleGui button not working when nested Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of PysimpleGui button not working when nested without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Here’s my code:

import PySimpleGUI as sg

layout1 = [[sg.Button('New', "center")], [sg.Button("Load Save", "center")]]
layout2 = [[sg.Text("2nd Page")], [sg.Button("New Page", "center")], [sg.Button("Load Page", "center")]]

layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-')]]

window = sg.Window('ORIGINAL').Layout(layout)

while True:             # Event Loop
    event, values = window.Read()
    if event in (None, 'Exit'):
        break
    if event == 'New':
        print("hello")
    elif event == '2':
        print("hello 2")
window.Close()

When I click on the buttons nothing happens. If I put those buttons inside the layout (instead of layout1 or layout2) they work perfectly. So my question is how can I get those buttons to work while leaving them where they are?

Sorry if it is a dumb question, I am pretty new to pysimplegui

Answer

The problem is not about where the button elements, but what arguments you provided to sg.Button.

sg.Button is defined as

class Button(Element):
    """
    Button Element - Defines all possible buttons. The shortcuts such as Submit, FileBrowse, ... each create a Button
    """

    def __init__(self, button_text='', button_type=BUTTON_TYPE_READ_FORM, target=(None, None), tooltip=None,
                 file_types=(("ALL Files", "*.*"),), initial_folder=None, default_extension='', disabled=False, change_submits=False,
                 enable_events=False, image_filename=None, image_data=None, image_size=(None, None),
                 image_subsample=None, border_width=None, size=(None, None), s=(None, None), auto_size_button=None, button_color=None,
                 disabled_button_color=None,
                 highlight_colors=None, mouseover_colors=(None, None), use_ttk_buttons=None, font=None, bind_return_key=False, focus=False, pad=None, key=None,
                 k=None, right_click_menu=None, expand_x=False, expand_y=False, visible=True, metadata=None):

Keyword omitted when you passed arguments to sg.Button, second argument will be button_type, not sure why you gave a 'center'. Besides button_text, key or key, it is clear that there’s no any other argument can be with such a value 'center'.

import PySimpleGUI as sg

layout1 = [
    [sg.Button('New')],
    [sg.Button('Load')],
    [sg.Button('Save')],
]

layout2 = [
    [sg.Text("2nd Page")],
    [sg.Button("New Page")],
    [sg.Button("Load Page")],
]

layout = [
    [sg.Column(layout1, key='-COL1-'),
     sg.Column(layout2, visible=False, key='-COL2-')],
]

window = sg.Window('ORIGINAL', layout)

while True:

    event, values = window.read()

    if event  == sg.WINDOW_CLOSED:
        break
    elif event == 'New':
        print("Hello New")
    elif event == 'Load':
        print("Hello Load")
    elif event == 'Save':
        print("Hello Save")

window.close()
We are here to answer your question about PysimpleGui button not working when nested - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji