In this tutorial, we will learn how to develop a database using Python with the help of PySimpleGUI library, and we will focus on customizing input fields. PySimpleGUI is a simple yet powerful GUI library for Python that allows you to create graphical user interfaces easily and quickly.
Before we begin, make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/downloads/). Also, you will need to install the PySimpleGUI library using the following command in your terminal or command prompt:
pip install PySimpleGUI
Now that we have everything set up, let’s get started with developing a database using Python with custom input fields.
Step 1: Import necessary libraries
First, we need to import the necessary libraries – PySimpleGUI and SQLite. SQLite is a lightweight database that we will use to store our data.
import PySimpleGUI as sg
import sqlite3
Step 2: Create a database and connect to it
Next, we need to create a database and connect to it using SQLite.
conn = sqlite3.connect('database.db')
c = conn.cursor()
# Create a table in the database
c.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)''')
conn.commit()
Step 3: Create a GUI window with custom input fields
Now, let’s create a GUI window with custom input fields for the user to enter data.
layout = [
[sg.Text('Name:'), sg.Input(key='name')],
[sg.Text('Email:'), sg.Input(key='email')],
[sg.Button('Add User'), sg.Button('Show Users')]
]
window = sg.Window('Database App', layout)
In this code snippet, we have created a window with two input fields for name and email, along with two buttons – ‘Add User’ and ‘Show Users’.
Step 4: Process user input
We need to write code to process user input when the buttons are clicked. Let’s add the following code below the GUI window creation code.
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Add User':
name = values['name']
email = values['email']
c.execute("INSERT INTO users (name, email) VALUES (?, ?)", (name, email))
conn.commit()
sg.popup('User added successfully.')
if event == 'Show Users':
c.execute("SELECT * FROM users")
users = c.fetchall()
for user in users:
print(user)
window.close()
conn.close()
In this code snippet, we have added a while loop to continuously check for events in the GUI window. When the ‘Add User’ button is clicked, we extract the values from the input fields and insert them into the database. Similarly, when the ‘Show Users’ button is clicked, we fetch all users from the database and print them.
Step 5: Run the program
Finally, let’s run the program and test our database application with custom input fields.
if __name__ == '__main__':
conn = sqlite3.connect('database.db')
c = conn.cursor()
layout = [
[sg.Text('Name:'), sg.Input(key='name')],
[sg.Text('Email:'), sg.Input(key='email')],
[sg.Button('Add User'), sg.Button('Show Users')]
]
window = sg.Window('Database App', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Add User':
name = values['name']
email = values['email']
c.execute("INSERT INTO users (name, email) VALUES (?, ?)", (name, email))
conn.commit()
sg.popup('User added successfully.')
if event == 'Show Users':
c.execute("SELECT * FROM users")
users = c.fetchall()
for user in users:
print(user)
window.close()
conn.close()
That’s it! You have successfully developed a database using Python with custom input fields using PySimpleGUI. Feel free to experiment with the code and customize it further to meet your requirements. Happy coding!