PySimpleGUI is a Python library that allows you to easily create GUI applications in no time. In this tutorial, I will show you how to create a complete CRUD (Create, Read, Update, Delete) application using PySimpleGUI that allows you to interact with a database in just seconds.
To follow along with this tutorial, make sure you have PySimpleGUI installed on your machine. You can install it using pip by running the following command:
pip install PySimpleGUI
Next, we will set up a SQLite database to store our data. SQLite is a lightweight and easy-to-use database engine that is perfect for small-scale applications. You can create a new SQLite database file using the following code:
import sqlite3
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)
''')
conn.commit()
conn.close()
This code creates a new SQLite database file called database.db
and creates a table called users
with three columns – id
, name
, and email
.
Now, let’s start building our PySimpleGUI application. We will create a GUI window with input fields for the user’s name and email address, as well as buttons to add, update, and delete users.
import PySimpleGUI as sg
layout = [
[sg.Text('Name:'), sg.Input(key='name')],
[sg.Text('Email:'), sg.Input(key='email')],
[sg.Button('Add'), sg.Button('Update'), sg.Button('Delete')],
[sg.Output(size=(50, 10))]
]
window = sg.Window('CRUD Application', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Add':
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', (values['name'], values['email']))
conn.commit()
conn.close()
print(f'Added user: {values["name"]}, {values["email"]}')
if event == 'Update':
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('UPDATE users SET name = ?, email = ? WHERE id = ?', (values['name'], values['email'], values['id']))
conn.commit()
conn.close()
print(f'Updated user: {values["name"]}, {values["email"]}')
if event == 'Delete':
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM users WHERE id = ?', (values['id'],))
conn.commit()
conn.close()
print(f'Deleted user with id: {values["id"]}')
window.close()
This code creates a PySimpleGUI window with input fields for the user’s name and email address, as well as buttons to add, update, and delete users. The code inside the event handlers for each button performs the corresponding operation on the SQLite database.
You can now run the script and test the CRUD operations on your database. This example demonstrates how easy it is to create a complete CRUD application using PySimpleGUI in just seconds.
I hope you found this tutorial helpful in creating a PySimpleGUI script for CRUD operations. Feel free to customize the application further and add additional features as needed. Happy coding!
QUERO FAZER IGUAL PRESISO DE AJUDA
Queria uma ajuda, eu trabalho atualizando firmware de roteador. Gostaria de uma ajuda de como posso automatizar essa tarefa.
Q top. Queria aprender !