Tutorial 16: Creating a List Box GUI App with Python PySimpleGUI

Posted by


In this tutorial, we will be creating a List Box GUI app using PySimpleGUI in Python. PySimpleGUI is a simple yet powerful Python library that allows users to create GUI applications with ease.

To get started, make sure you have PySimpleGUI installed on your system. You can install it using pip by running the following command:

pip install PySimpleGUI

Once you have PySimpleGUI installed, create a new Python file and import the necessary libraries:

import PySimpleGUI as sg

Next, let’s define the layout of our GUI app. In this tutorial, we will create a simple window with a List Box widget. The List Box widget allows users to select one or more items from a list.

layout = [
    [sg.Listbox(values=['Item 1', 'Item 2', 'Item 3', 'Item 4'], size=(20, 5), key='-LISTBOX-')],
    [sg.Button('Ok'), sg.Button('Cancel')]
]

In the above layout, we have defined a List Box widget with four items (‘Item 1’, ‘Item 2’, ‘Item 3’, ‘Item 4’). We have also included two buttons (‘Ok’ and ‘Cancel’).

Now, let’s create the window using the layout we defined:

window = sg.Window('List Box Example', layout)

Next, let’s create a loop to handle events and update the GUI window. Inside the loop, we will wait for user input and respond accordingly.

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Cancel'):
        break
    if event == 'Ok':
        selected_items = values['-LISTBOX-']
        sg.popup(f'You selected: {selected_items}')

In the above code, we are checking for events such as closing the window or clicking the ‘Cancel’ button. If the ‘Ok’ button is clicked, we retrieve the selected items from the List Box widget and display them using a popup message.

Finally, don’t forget to close the window after the loop ends:

window.close()

Congratulations! You have successfully created a List Box GUI app using PySimpleGUI in Python. You can customize the layout and functionality of the app further to suit your needs.

I hope you found this tutorial helpful. If you have any questions or need further assistance, feel free to ask. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
2 months ago

✅ Python PySimpleGUI Full Playlist : https://www.youtube.com/playlist?list=PLMi6KgK4_mk1kGWXRif1naLejNdYDS_t2