![](https://crypto4nerd.com/wp-content/uploads/2023/12/1APeFmOpyU5zPsoo97poFnQ-1024x752.png)
Installing PySimpleGUI
To install PySimpleGUI, you can use pip:
pip install pysimplegui
Alternatively, you can download the PySimpleGUI.py file from the GitHub repository and place it in your project folder.
Creating Your First GUI :
To create your first GUI with PySimpleGUI, you need to import the library and define your layout. A layout is a list of lists that contains the elements you want to show in your window. Each sublist represents a row of elements. You can use various elements such as Text, Input, Button, Checkbox, Radio, Slider, etc. You can refer to the documentation for the full list of elements and their options.
For example, let’s create a simple GUI that asks for your name and greets you. The layout will consist of two rows: one with a Text element and an Input element for entering your name, and another with a Button element for submitting your name.
import PySimpleGUI as sg
layout = [
[sg.Text("Enter your name:"), sg.Input(key="-NAME-")],
[sg.Button("Submit")]
]
Notice that we used the key parameter to assign a unique identifier to the Input element. This will help us access its value later.
Next, we need to create a window object using the layout. We can also specify some optional parameters such as the title, size, location, etc. of the window.
window = sg.Window("Hello World", layout)
Now we have our window ready, but we need to add some logic to handle the user interaction. We can use an event loop to read the events and values from the window. An event is something that happens in the window, such as clicking a button or closing the window. A value is the current state of an element, such as the text entered in an input field or the checked state of a checkbox.
We can use the window.read() method to get the event and values from the window. This method will block until an event occurs. Then we can use conditional statements to perform different actions based on the event and values.
For example, if the event is “Submit”, we can get the value of the input element using its key and display a popup message that greets the user. If the event is None or “Exit”, we can break out of the loop and close the window.
while True:
event, values = window.read()
if event == "Submit":
name = values["-NAME-"]
sg.popup(f"Hello {name}!")
if event in (None, "Exit"):
break
Finally, we need to close the window after exiting the loop.
window.close()
That’s it! We have created our first GUI with PySimpleGUI. Here’s the complete code:
import PySimpleGUI as sg
layout = [
[sg.Text("Enter your name:"), sg.Input(key="-NAME-")],
[sg.Button("Submit")]
]
window = sg.Window("Hello World", layout)
while True:
event, values = window.read()
if event == "Submit":
name = values["-NAME-"]
sg.popup(f"Hello {name}!")
if event in (None, "Exit"):
breakwindow.close()
And here’s how the GUI looks like:
Now that you know the basic steps to create a GUI with PySimpleGUI, you can experiment with different elements, layouts, and logic to create more GUIs. Here are some examples of GUIs that you can create with PySimpleGUI.
A Calculator
A calculator is a common example of a GUI application. To create a calculator with PySimpleGUI, we can use a combination of Text, Input, and Button elements. We can also use some math functions from the built-in math module.
The layout will consist of four rows: one with a Text element to show the result, one with an Input element to enter the expression, and two with Button elements to perform the calculation or clear the input.
import PySimpleGUI as sg
import mathlayout = [
[sg.Text("0", size=(20, 1), justification="right", key="-RESULT-")],
[sg.Input(key="-INPUT-")],
[sg.Button("Calculate"), sg.Button("Clear")],
[sg.Button("Sin"), sg.Button("Cos"), sg.Button("Tan"), sg.Button("Log")]
]
Notice that we used the size and justification parameters to adjust the appearance of the Text element. We also used the key parameter to assign identifiers to the Text and Input elements.
Next, we need to create a window object using the layout.
window = sg.Window("Calculator", layout)
Then, we need to add an event loop to handle the user interaction. We can use the window.read() method to get the event and values from the window. Then we can use conditional statements to perform different actions based on the event and values.
For example, if the event is “Calculate”, we can get the value of the input element using its key and evaluate it using the built-in eval function. We can then update the text of the result element using its key and the window[key].update(value) method. If the event is “Clear”, we can clear both the input and result elements using the same method. If the event is one of the math functions, we can append it to the input element using the window[key].update(value, append=True) method. If the event is None or “Exit”, we can break out of the loop and close the window.
while True:
event, values = window.read()
if event == "Calculate":
expression = values["-INPUT-"]
try:
result = eval(expression)
window["-RESULT-"].update(result)
except:
window["-RESULT-"].update("Invalid expression")
if event == "Clear":
window["-INPUT-"].update("")
window["-RESULT-"].update("0")
if event in ("Sin", "Cos", "Tan", "Log"):
function = event.lower()
window["-INPUT-"].update(function + "(", append=True)
if event in (None, "Exit"):
break
Finally, we need to close the window after exiting the loop.
window.close()
That’s it! We have created a calculator with PySimpleGUI.
A To Do List
A to do list is another common example of a GUI application. To create a to do list with PySimpleGUI, we can use a combination of Text, Input, Listbox, and Button elements. We can also use some list operations to manipulate the tasks.
The layout will consist of five rows: one with a Text element to show the title, one with an Input element to enter a new task, one with a Listbox element to show the existing tasks, and two with Button elements to add or delete tasks.
import PySimpleGUI as sg
layout = [
[sg.Text("To Do List", size=(20, 1), font=("Arial", 20))],
[sg.Input(key="-NEW-TASK-")],
[sg.Listbox(values=[], size=(40, 10), key="-TASKS-")],
[sg.Button("Add"), sg.Button("Delete")],
[sg.Button("Exit")]
]
Notice that we used the size and font parameters to adjust the appearance of the Text element. We also used the key parameter to assign identifiers to the Input and Listbox elements. We initialized the Listbox element with an empty list of values.
Next, we need to create a window object using the layout.
window = sg.Window("To Do List", layout)
Then, we need to add an event loop to handle the user interaction. We can use the window.read() method to get the event and values from the window. Then we can use conditional statements to perform different actions based on the event and values.
For example, if the event is “Add”, we can get the value of the input element using its key and append it to a list of tasks. We can then update the values of the listbox element using its key and the window[key].update(value) method. If the event is “Delete”, we can get the value of the listbox element using its key and remove it from the list of tasks. We can then update the values of the listbox element using the same method. If the event is None or “Exit”, we can break out of the loop and close the window.
tasks = []
while True:
event, values = window.read()
if event == "Add":
new_task = values["-NEW-TASK-"]
if new_task:
tasks.append(new_task)
window["-TASKS-"].update(tasks)
window["-NEW-TASK-"].update("")
if event == "Delete":
selected_task = values["-TASKS-"][0]
if selected_task:
tasks.remove(selected_task)
window["-TASKS-"].update(tasks)
if event in (None, "Exit"):
break
Finally, we need to close the window after exiting the loop.
window.close()
That’s it! We have created a to-do list with PySimpleGUI.