Certainly! To create a Python GUI application for managing personal information, you can use the tkinter library for the GUI aspect and file handling for database-like functionalities. Here's a simple implementation:
import tkinter as tk
from tkinter import messagebox
import os
# Function to save record to a text file
def save_record():
info = (
f"Name: {name_var.get()}, "
f"Father's Name: {father_name_var.get()}, "
f"National Number: {national_number_var.get()}, "
f"Date of Birth: {dob_var.get()}, "
f"Educational Qualification: {education_var.get()}, "
f"Telephone: {telephone_var.get()}, "
f"Position: {position_var.get()}, "
f"Address: {address_var.get()}\n"
)
with open("database.txt", "a") as db_file:
db_file.write(info)
messagebox.showinfo("Success", "Record saved successfully")
clear_fields()
# Function to display all records
def display_records():
if os.path.exists("database.txt"):
with open("database.txt", "r") as db_file:
records = db_file.read()
records_display.delete(1.0, tk.END)
records_display.insert(tk.END, records)
else:
messagebox.showinfo("No Records", "No records found")
# Function to search records
def search_record():
key = search_var.get()
if os.path.exists("database.txt"):
with open("database.txt", "r") as db_file:
records = db_file.readlines()
matching_records = [record for record in records if key in record]
search_display.delete(1.0, tk.END)
if matching_records:
search_display.insert(tk.END, "".join(matching_records))
else:
search_display.insert(tk.END, "No matching records found")
else:
messagebox.showinfo("No Records", "No records found")
# Function to delete record by searching key
def delete_record():
key = search_var.get()
if os.path.exists("database.txt"):
with open("database.txt", "r") as db_file:
records = db_file.readlines()
with open("database.txt", "w") as db_file:
for record in records:
if key not in record.strip("\n"):
db_file.write(record)
messagebox.showinfo("Deletion", "Record(s) deleted if existed.")
else:
messagebox.showinfo("No Records", "No records to delete from")
# Function to clear input fields
def clear_fields():
name_var.set("")
father_name_var.set("")
national_number_var.set("")
dob_var.set("")
education_var.set("")
telephone_var.set("")
position_var.set("")
address_var.set("")
# Creating main window
root = tk.Tk()
root.title("Personal Information Manager")
# Input fields
name_var = tk.StringVar()
father_name_var = tk.StringVar()
national_number_var = tk.StringVar()
dob_var = tk.StringVar()
education_var = tk.StringVar()
telephone_var = tk.StringVar()
position_var = tk.StringVar()
address_var = tk.StringVar()
search_var = tk.StringVar()
tk.Label(root, text="Name").grid(row=0, column=0)
tk.Entry(root, textvariable=name_var).grid(row=0, column=1)
tk.Label(root, text="Father's Name").grid(row=1, column=0)
tk.Entry(root, textvariable=father_name_var).grid(row=1, column=1)
tk.Label(root, text="National Number").grid(row=2, column=0)
tk.Entry(root, textvariable=national_number_var).grid(row=2, column=1)
tk.Label(root, text="Date of Birth").grid(row=3, column=0)
tk.Entry(root, textvariable=dob_var).grid(row=3, column=1)
tk.Label(root, text="Educational Qualification").grid(row=4, column=0)
tk.Entry(root, textvariable=education_var).grid(row=4, column=1)
tk.Label(root, text="Telephone Number").grid(row=5, column=0)
tk.Entry(root, textvariable=telephone_var).grid(row=5, column=1)
tk.Label(root, text="Position").grid(row=6, column=0)
tk.Entry(root, textvariable=position_var).grid(row=6, column=1)
tk.Label(root, text="Address").grid(row=7, column=0)
tk.Entry(root, textvariable=address_var).grid(row=7, column=1)
# Buttons
tk.Button(root, text="Save Record", command=save_record).grid(row=8, column=0)
tk.Button(root, text="Display Records", command=display_records).grid(row=8, column=1)
tk.Button(root, text="Clear Fields", command=clear_fields).grid(row=8, column=2)
# Search and Delete Section
tk.Label(root, text="Search").grid(row=9, column=0)
tk.Entry(root, textvariable=search_var).grid(row=9, column=1)
tk.Button(root, text="Search Record", command=search_record).grid(row=9, column=2)
tk.Button(root, text="Delete Record", command=delete_record).grid(row=9, column=3)
# Display areas for records and search results
records_display = tk.Text(root, height=10, width=50)
records_display.grid(row=10, column=0, columnspan=4)
search_display = tk.Text(root, height=5, width=50)
search_display.grid(row=11, column=0, columnspan=4)
# Run the main loop
root.mainloop()
Explanation:
- Library: We use the
tkinter library for the GUI components and messageboxes.
- File Handling: Data is saved to
database.txt. Functions are provided to save, display, search, and delete records.
- Widgets: Labels, entry fields, buttons, and text widgets are used to build the GUI.
- Functions: Create various functions to handle button events for saving, displaying, searching, and deleting records.
- Database Persistence: The data is stored in a text file, which acts as a simple database for the purpose of this application.
This code is basic and meant for educational purposes to illustrate the key concepts of Tkinter GUI development and file handling in Python. For a production-ready solution, consider using a more robust data storage solution like SQLite or a proper database system and employ security practices, especially when dealing with sensitive data.