FreeSimpleGUI is a Python library that makes creating graphical user interfaces (GUIs) for your applications a breeze. It is a fork of PySimpleGUI which is now a paid library to use while FreeSimpleGUI is free. FreeSimpleGUI offers a simple and intuitive API, allowing you to build user-friendly interfaces without getting bogged down in complex code. This tutorial will guide you through the basics of FreeSimpleGUI, helping you create your first simple GUI application.
Installation:
The first step is to install FreeSimpleGUI using pip:
pip install FreeSimpleGUI
Basic Elements:
FreeSimpleGUI provides various pre-built elements to construct your GUI. Here are some commonly used ones:
Creating a Simple Window:
Let's build a basic window with a text element and a button:
import FreeSimpleGUI as sg
# Define the layout
layout = [[sg.Text("Hello, World!"), sg.Button("Click Me")]]
# Create the window
window = sg.Window("My First GUI", layout)
# Event Loop
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "Click Me":
print("Button clicked!")
window.close()
Explanation:
FreeSimpleGUI
as sg
for convenience.layout
list defines the window's content. In this case, it has a row with a text element and a button.sg.Window
function creates the window with the specified title ("My First GUI") and the layout.while True
loop keeps the window open until the user closes it.window.read()
pauses the program and waits for user interaction (closing the window or clicking the button). It returns a tuple containing the event name (e.g., button click, window closed) and the current values of all input elements (empty in this case).if
statements check for the event type:sg.WIN_CLOSED
: The user closed the window, so we break out of the loop.window.close()
closes the window after the loop exits.Running the Script:
Save the code as a Python file (e.g., my_window.py
) and run it from the terminal:
python my_window.py
This will display a window with the text "Hello, World!" and a button labeled "Click Me." Clicking the button will print "Button clicked!" to the console.
Adding Input Fields:
Let's modify the example to include an input field where users can enter their name and a button to greet them:
import FreeSimpleGUI as sg
layout = [
[sg.Text("Enter your name:")],
[sg.InputText(key="name")],
[sg.Button("Greet Me")]
]
window = sg.Window("Greeting App", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "Greet Me":
name = values["name"]
print(f"Hello, {name}!")
window.close()
Here, we've added an sg.InputText
element with the key "name" to capture user input. In the event loop, we retrieve the entered name from the "values" dictionary and use it to create a personalized greeting message.
Customizing the Look and Feel:
FreeSimpleGUI allows some basic customization options for the window and elements. Here are a couple of examples:
size
argument of sg.Window
to set the width and height in pixels (e.g., sg.Window("Window", layout, size=(600, 400))
).font
argument within element creation functions to set the font and size (e.g., sg.Text("Text", font=("Arial", 12))
).FreeSimpleGUI
as sg
for convenience.layout
list defines the window's content. In this case, it has a row with a text element and a button.sg.Window
function creates the window with the specified title ("My First GUI") and the layout.while True
loop keeps the window open until the user closes it.window.read()
pauses the program and waits for user interaction (closing the window or clicking the button). It returns a tuple containing the event name (e.g., button click, window closed) and the current values of all input elements (empty in this case).if
statements check for the event type:sg.WIN_CLOSED
: The user closed the window, so we break out of the loop.window.close()
closes the window after the loop exits.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.