Frontend Development 7 min read

Creating Dialog Windows and a Simple Zip Utility with PySimpleGUI

This tutorial demonstrates how to install PySimpleGUI, design one‑shot and persistent windows, use various popup functions for messages, text input, file and folder selection, progress meters, and finally combine these features to build a lightweight Python zip‑compression tool with full code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating Dialog Windows and a Simple Zip Utility with PySimpleGUI

PySimpleGUI is introduced as a convenient Python library for building graphical user interfaces, especially useful in automation and operations tasks.

Installation : run pip install pysimplegui in the terminal, then verify with import PySimpleGUI in a Python session.

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 application window.

Basic pop‑up functions (all prefixed with sg. ):

sg.popup('Notice!')

sg.popup_ok('Default popup')

sg.popup_yes_no('Yes/No popup')

sg.popup_cancel('Cancel button popup')

sg.popup_ok_cancel('OK/Cancel popup')

sg.popup_error('Error popup')

sg.popup_auto_close('Auto‑close after seconds')

Text‑scrolling popup uses sg.popup_scrolled(text, title='Hello') to display multi‑line messages.

User input popups :

sg.popup_get_text('Enter text') returns the entered string or None on cancel.

For password‑style input, the same function can hide characters.

File and folder selection :

sg.popup_get_file('Select file') lets the user pick a file and returns its path.

sg.popup_get_folder('Select folder') returns the chosen directory.

Progress meter example:

<code>for i in range(1000):
    sg.one_line_progress_meter('Progress', i+1, 1000, 'key', 'Progress bar')
</code>

Both horizontal and vertical orientations can be set, along with custom colors.

Putting it all together – a simple compression tool :

The application asks the user to choose a folder, then a destination for the zip file, compresses all files in the selected folder using the zipfile module, and finally shows the resulting archive size.

<code>import PySimpleGUI as sg
import zipfile
import os

folder = sg.popup_get_folder('请选择要压缩的文件夹')
zip_path = sg.popup_get_file(
    '请选择要保存的压缩包位置',
    save_as=True,
    default_extension='zip',
    file_types=(('压缩包', '.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'压缩包体积大小为:{zip_size} KB')
</code>

The tutorial includes screenshots of each popup type and the final result, illustrating how PySimpleGUI can streamline GUI creation for Python scripts.

GUIPythonAutomationfile compressionDialogPySimpleGUI
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.