Create an advanced GUI app with PySimpleGUI (Full Tutorial)
Introduction
In this tutorial, I’ll walk you through creating a GUI application using Python. The demo application allows users to select an Excel spreadsheet and convert it into a CSV file. However, the main focus is to demonstrate how to create a GUI that can save and load user settings. This means that when I enter a value, click the save settings button, and close the app, I can see the changes the next time I open it. Additionally, we will implement a custom menu bar to enhance our application.
Basic example
First, we need to install the required dependency for this project, which is the PySimpleGUI package. Open your terminal or command prompt, type pip install pysimplegui
, and hit enter. Once installed, I will open a blank Python file and import PySimpleGUI as sg
. I will then define the layout of our application, where each list in the layout represents a row. For instance, the first row will contain some text, an input field, and a button to browse for files, while the second will allow browsing for a folder.
Add file types to the browse button
To ensure the user can only select Excel files, I will specify the file_types
parameter. This parameter takes a tuple of tuples as input, where the first tuple defines the label (‘Excel files’) and the second specifies the file types (e.g., ‘xls’). After this implementation, when I click the browse button, I can now only select Excel files.
Convert the Excel file to CSV
Next, I will install the pandas library to help convert the selected Excel file into a CSV format. This requires the command pip install pandas
and also the optional dependency openpyxl
for manipulating Excel files. After importing pandas as pd
, I will create a function named convert_to_CSV
which takes arguments for the Excel file path, output folder, sheet name, separator, and decimal value. This function will read the Excel file into a pandas DataFrame and convert it to a CSV file.
Display the content of the Excel file
To enhance the user experience, I will add a button labeled “Display Excel File.” This button will allow users to view the content of the Excel file directly in the application. When clicked, it will display the data using a popup_scrolled
element, including the data types for each column. Additionally, I will implement a validation function to ensure the provided file path is correct and exists to prevent application crashes.
Validate file paths
File validation is crucial for a good user experience. I will implement a function to check if the file path is not empty and if it exists. If the path is incorrect, an error message will be displayed. This validation will be applied before displaying Excel data or converting it to CSV.
Implement the settings function
To allow for dynamic changes in our application, I will create a settings.config
file. This file will define different sections for GUI settings (like title, font size, font family, and theme), CSV manipulation settings (separator and decimal symbol), and the Excel file’s sheet name. The application will load these settings upon startup and allow users to modify them directly from the GUI.
Create the settings window
When users click the settings button, a modal window will open, displaying the current settings. Users can modify these settings, and upon clicking “Save Current Settings,” the new values will be written back to the config file. This process involves reading the values from the input fields and updating the config file accordingly.
Add a custom menu bar
To enhance our application further, I will implement a custom menu bar using sg.MenubarCustom
. The menu will include options like ‘Help’ and ‘About’, and clicking ‘About’ will display additional information about the application. The menu can be easily integrated into the layout, and its functionality will be handled in the event loop.
Adjust the styling and clean up the code
As a final touch, I will clean up the code and adjust the styling of the GUI elements. This includes setting a custom title bar color, adjusting button sizes, and ensuring consistency in element alignment. Additionally, I will use short forms for the different elements to make the code cleaner.
Bonus: Convert the GUI to an executable file
Lastly, I will show you how to convert the Python application into a standalone executable file using the psgcompiler
. This requires creating a separate virtual environment to avoid bloating the executable with unnecessary packages. After selecting the Python file in the psgcompiler, the conversion process will create an executable that can be shared with users who do not have Python installed.
Outro
Congratulations! You have successfully created a custom GUI application with a menu bar and settings file using PySimpleGUI. Feel free to explore and modify the code to suit your needs. Let me know in the comments what kind of programs you plan to build with the PySimpleGUI package!
Further Links
- Source Code: GitHub Repository
- PySimpleGUI Documentation: Official Documentation