Tutorial #06: File browsing, table creation, and more with Python PySimpleGUI

Posted by


In this tutorial, we will explore how to use PySimpleGUI to create a file browse dialog, display data in a table using PySimpleGUI’s table element, as well as some additional features such as changing themes and customizing window layouts.

Step 1: Install PySimpleGUI
First, make sure you have PySimpleGUI installed on your system. You can install it using pip:

pip install PySimpleGUI

Step 2: Create a File Browse Dialog
To create a file browse dialog, we first need to import PySimpleGUI and create a layout for our window. Here’s an example code snippet to create a simple file browse dialog:

import PySimpleGUI as sg

layout = [
    [sg.InputText(), sg.FileBrowse(button_text='Browse')],
    [sg.Button('Ok'), sg.Button('Cancel')]
]

window = sg.Window('File Browse Dialog', layout)

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Cancel'):
        break
    elif event == 'Ok':
        print('Selected File:', values[0])

window.close()

In this code snippet, we create a window with an input text field and a file browse button. When the file browse button is clicked, a file dialog will open allowing the user to select a file. The selected file path will be displayed in the input text field.

Step 3: Create a Table
Next, let’s create a table to display data. PySimpleGUI provides a table element that makes it easy to display tabular data. Here’s an example code snippet:

import PySimpleGUI as sg

data = [
    ['John', 25, 'Chicago'],
    ['Sarah', 28, 'New York'],
    ['Michael', 30, 'Los Angeles']
]

layout = [
    [sg.Table(data, headings=['Name', 'Age', 'City'])],
    [sg.Button('Ok'), sg.Button('Cancel')]
]

window = sg.Window('Table Example', layout)

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Cancel'):
        break

window.close()

In this code snippet, we create a table with sample data (name, age, city) and display it in a window. The table will have column headings ‘Name’, ‘Age’, and ‘City’.

Step 4: Change Theme and Customize Window Layout
PySimpleGUI allows you to easily change the theme of your GUI application. You can choose from a variety of themes provided by PySimpleGUI. Here’s how you can change the theme and customize the window layout:

import PySimpleGUI as sg

sg.theme('LightGreen')

layout = [
    [sg.Text('Enter your name:'), sg.InputText()],
    [sg.Button('Submit')]
]

window = sg.Window('Custom Theme Example', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

window.close()

In this code snippet, we change the theme to ‘LightGreen’ and customize the layout with a text input field and a submit button.

That’s it for this tutorial on using PySimpleGUI for file browse, table display, changing themes, and customizing window layouts. I hope you found this tutorial helpful! Feel free to explore more features and functionalities of PySimpleGUI to create powerful and interactive GUI applications with Python.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@OsmanDayi
1 month ago

Only 33 likes? Oh man I'm sorry for that.

@pi8anotita
1 month ago

very good tutorials. My question is if we use this Gui to add data where are the data stored? how can we find the data file so we can get meta data?

@edappalkkaran
1 month ago

how we contol the calendar position on the screen