PySimpleGUI

PySimpleGUI graphical interfaces for creation of software.


In the command line / terminal type: pip install pysimplegui wait for the installation to complete, enter the python environment, type import PySimpleGUI and confirm the successful installation.

PySimpleGUI can use several bindings, one of them is tk. To install tk you can use the command sudo apt install python3.6-tk. Other bindings include web and qt.

Related course: Desktop Apps made Simple with Python (PySimpleGUI)

Create a simple pop-up window interface. The default library abbreveation is sg, and it is recommended to use this name when using it.

import PySimpleGUI as sg
sg.popup("Hello World")

PySimpleGUI popup

There are many popup types, a demo is shown below.

sg.popup('Attention!')
sg.popup_ok('Default popup')
sg.popup_yes_no('Popup with Yes and No buttons')
sg.popup_cancel('Popup with cancel button')
sg.popup_ok_cancel('Popup with OK and cancel buttons')
sg.popup_error('popup with red error button')
sg.popup_auto_close('Popup window that closes automatically after a few seconds')

You can set custom parameters. For example, setting up a small customized window:

sg.popup(
    'window',
    title='Hello',
    button_color=('#A81B0C', '#FFFFFF'),
    background_color='#F47264',
    line_width=2,
    custom_text='text'
)

Text content popup window

Use the popup_scrolled() method, and add the content to be displayed in parentheses

text = "Hello"
sg.popup_scrolled(text,title='Hello')

popup with text

Get user input

Using the PySimpleGUI popup_get_text() method, you can get user input. It is somewhat similar to the prompt in the input() statement, reminding the user to enter text.

text1 = sg.popup_get_text('Enter text1: ')
print(text1)
text2 = sg.popup_get_text('Enter text2: ')
print(text2)

User input with GUI

Test for hidden password input, you can get the user input password in the form of a pop-up window:

text = sg.popup_get_text('Enter password', password_char='*')
sg.popup(f'Password:{text}')

Password input in Python

File selection popup window

Use the sg.popup_get_file() method to select a file.

path = sg.popup_get_file('Select a file')
print(path)

Open file with PySimpleGUI

You can add paramters:

  • message
  • default_path
  • save_as (bool)
  • multiple_files (bool)
  • file_types
  • initial_folder

Note

You can change those parameters directly in the function call

path = sg.popup_get_file( 'Select a file', save_as=True, default_extension='zip', file_types(('ZIP files´,'.zip'), ) ) print(path)

Select directory window

The sg.popup_get_folder() method is used as an input prompt to select a folder. After executing the program, a window for selecting files will pop up, and the path of the folder will be added to the input box after selection.

path = sg.popup_get_folder('Select folder')
print(path)

select folder

Progress bar pop-up window

Use the sg.one_line_progress_meter() method, and enter the relevant parameter settings in parentheses.

for i in range(1000):
    sg.one_line_progress_meter(
        'progress_bar',
        i + 1,
        1000,
        'this progress bar key',
        'this is a progress bar'
    )

Progressbar GUI with Python

You can change the orientation and colors

for i in range(1, 1000):
    sg.one_line_progress_meter(
        'progress_bar',
        i + 1,
        1000,
        'this progress bar key',
        'this is a progress bar',
        orientation='h',
        bar_color=('#F47264', '#FFFFFF')
    )

Horizontal progressbar with changed colors