Skip to content

[Question] How to handle events in event loop ? #4292

Description

@ViviNewbie

Type of Issue (Enhancement, Error, Bug, Question)

Can't cycle the layout with the button


Operating System

Windows 10

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

Version information can be obtained by calling sg.main_get_debug_data()
Or you can print each version shown in ()

Python version (sg.sys.version)

3.8.0

PySimpleGUI Version (sg.__version__)

4.41.2

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)


Your Experience In Months or Years (optional)

1.5 Years Python programming experience

1.5 Years Programming experience overall

Have used another Python GUI Framework? (tkinter, Qt, etc) (no is fine)

Anything else you think would be helpful? i'm a beginner and I'm not a developer. I use python for statistical analysis usually but am starting a new project for the students


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • [x ] Searched main docs for your problem www.PySimpleGUI.org
  • [x ] Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x ] Run your program outside of your debugger (from a command line)
  • [x ] Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • [x ] Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

Hi everyone,
I managed to perform cycle layout thanks to your help but I am stuck with a problem for a while now.
I think my last loop is not good: I would like when the user answers "E", to update the output with "good answer". Then the user has a to click on OK to go back to the first layout.
But it doesn't work: it creates an endless loop.
Why is my loop wrong?

Code To Duplicate

A short program that isolates and demonstrates the problem (Do not paste your massive program, but instead 10-20 lines that clearly show the problem)

This pre-formatted code block is all set for you to paste in your bit of code:

import PySimpleGUI as sg
# import PySimpleGUIQt as sg

# ----------- Create the 3 layouts this Window will display -----------
layout1 = [[sg.Text('Layout 1')],
           [sg.CB("A", key="A"), sg.CB("B", key="B")]]

layout2 = [[sg.Text('Layout 2')],
           [sg.CB("C", key="C"), sg.CB("D", key="D")]]

layout3 = [[sg.Text('Layout 3')],
           [sg.CB("E", key="E"), sg.CB("F", key="F")],
           [sg.Text(key="OUTPUT")]]

# ----------- 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('OK'), sg.Button('Exit')]]

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

layout = 1  # The currently visible layout
while True:
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    if event == 'OK' and values["A"]==True and values["B"]==False:
        window[f'-COL1-'].update(visible=False)
        window[f'-COL2-'].update(visible=True)
        if event=="OK" and values["C"]==True and values["D"]==False:
            window[f'-COL2-'].update(visible=False)
            window[f'-COL3-'].update(visible=True)
            if event =="OK" and values["E"]==True and values["F"]==False:
                window['OUTPUT'].update("Good answer")
                if event=="OK":
                    window[f'-COL3-'].update(visible=False)
                    window[f'-COL1-'].update(visible=True)
                
    
window.close()

Screenshot, Sketch, or Drawing

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions