Mastering PySimpleGUI: Build Interactive Dialogs and Simple Compression Tools in Python
This tutorial explains how to install PySimpleGUI, create various popup windows—including one‑shot, persistent, text‑scrolling, input, file and folder selectors—as well as a progress bar, and then combine these elements to build a simple Python compression utility that zips a chosen folder and reports its size.
Python plays a key role in operations and office automation; PySimpleGUI is a handy module that simplifies creating GUI dialogs for everyday tasks.
1. Installing PySimpleGUI
Run pip install pysimplegui in the terminal, then import with import PySimpleGUI to verify the installation.
2. Creating simple popup windows
2.1 Two window design patterns
One‑shot window: appears once, suitable for notifications or data collection.
Persistent window: stays open until the user closes it, often used as the main interface.
2.2 Popup examples
Common popup functions (sg is the default alias):
sg.popup('Attention!') sg.popup_ok('Default popup') sg.popup_yes_no('Yes/No buttons') sg.popup_cancel('Cancel button') sg.popup_ok_cancel('OK and Cancel') sg.popup_error('Error style') sg.popup_auto_close('Auto‑close after seconds')The last example closes automatically after about 2 seconds. All popups accept additional parameters such as title, colors, line width, and custom button text.
sg.popup(
'This is a popup',
title='Hello',
button_color=('#A81B0C', '#FFFFFF'),
background_color='#F47264',
line_width=2,
custom_text=' OK '
)2.3 Text‑scrolling popup
Use sg.popup_scrolled(text, title='Hello') to display multi‑line text.
text = '''Hello everyone,
let's learn PySimpleGUI together.'''
sg.popup_scrolled(text, title='Hello')2.4 Getting user input
Use sg.popup_get_text() to prompt for input; the function returns the entered string or None if cancelled.
text1 = sg.popup_get_text('Enter text 1')
print(text1)
text2 = sg.popup_get_text('Enter text 2')
print(text2)2.5 File selection
Use sg.popup_get_file() to let the user choose a file path; the selected path appears in the input box.
2.6 Folder selection
Use sg.popup_get_folder() to let the user pick a directory; the chosen path is returned.
2.7 Progress bar
Use sg.one_line_progress_meter() inside a loop to display a dynamic progress bar.
for i in range(1000):
sg.one_line_progress_meter('Progress', i+1, 1000, 'key', 'Progress bar')3. Building a simple compression utility
3.1 Requirements
Show a folder‑selection popup.
Show a file‑save popup for the zip archive.
Compress all files in the chosen folder.
Display the resulting archive size.
3.2 Implementation
The script uses sg.popup_get_folder(), sg.popup_get_file(save_as=True, default_extension='zip'), the standard zipfile module, and os.stat() to report size.
import PySimpleGUI as sg
import zipfile
import os
folder = sg.popup_get_folder('Select folder to compress')
zip_path = sg.popup_get_file('Select save location',
save_as=True,
default_extension='zip',
file_types=(('zip archive', '.zip'),))
with zipfile.ZipFile(zip_path, 'w') as zipobj:
for file in os.scandir(folder):
zipobj.write(file.path, file.path.replace(folder, '.'))
zip_size = os.stat(zip_path).st_size // 1024
sg.popup(f'Compressed size: {zip_size} KB')Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
