Experimental Faster Version: Using Code to Generate Layouts with PySimpleGUI 2020 (Part 10)

Posted by


In this tutorial, we will explore how to generate layouts using code in PySimpleGUI 2020. This feature is still experimental and faster than the previous method of defining layouts, making it a great option for creating dynamic and complex GUIs.

To get started, make sure you have PySimpleGUI version 4.25.0 or later installed. You can install it using pip:

pip install PySimpleGUI

Now, let’s create a simple GUI using the generated layout feature. We will create a window with a text input field and a button. When the button is clicked, it will display the text entered in the input field in a popup window.

Here is the code to achieve this:

import PySimpleGUI as sg

layout = [
    [sg.Text('Enter some text:')],
    [sg.Input(key='-INPUT-')],
    [sg.Button('Submit')],
]

window = sg.Window('Generated Layout Example', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Submit':
        text = values['-INPUT-']
        sg.popup(f'You entered: {text}')

window.close()

In this code, we define the layout using a list of lists. Each inner list represents a row in the layout, and each element in the inner list represents a widget (e.g. Text, Input, Button). The ‘key’ parameter is used to identify each widget, so we can access its value later.

Next, we create a window using the layout and enter a loop to handle events and update the GUI. The window.read() method returns the event that occurred and the values of the widgets. We check for the WIN_CLOSED event to close the window and the ‘Submit’ event to display the entered text in a popup window.

Now, let’s take a look at a more complex example using the generated layout feature. We will create a window with a listbox, a text input field, and a button. When an item is selected in the listbox and the button is clicked, it will display the selected item and the text entered in the input field in a popup window.

import PySimpleGUI as sg

layout = [
    [sg.Listbox(['Apple', 'Banana', 'Cherry'], key='-LISTBOX-')],
    [sg.Text('Enter some text:')],
    [sg.Input(key='-INPUT-')],
    [sg.Button('Submit')],
]

window = sg.Window('Generated Layout Example', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Submit':
        selected_item = values['-LISTBOX-'][0]
        text = values['-INPUT-']
        sg.popup(f'Selected item: {selected_item}nEntered text: {text}')

window.close()

In this code, we added a listbox widget with a list of items (‘Apple’, ‘Banana’, ‘Cherry’). When an item is selected in the listbox, its value is stored in the ‘-LISTBOX-‘ key. We retrieve the selected item and the entered text using their respective keys and display them in the popup window.

Generated layouts using code are a powerful feature in PySimpleGUI 2020 that allows you to create dynamic and complex GUIs with ease. Experiment with different widgets, layouts, and functionalities to build interactive applications tailored to your needs. Remember that this feature is still experimental, so updates and improvements may be made in future versions of PySimpleGUI.

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@triangulatorr4559
2 hours ago

OK, so the combination of concatenation and list comprehension is really powerful!

1
0
Would love your thoughts, please comment.x
()
x