Skip to content
Snippets Groups Projects
Commit 8b1bef69 authored by Bui2.Huan@live.uwe.ac.uk's avatar Bui2.Huan@live.uwe.ac.uk
Browse files

Upload New File

parent 888c9b6f
No related branches found
No related tags found
No related merge requests found
import sqlite3
import json
import tkinter as tk
from tkinter import simpledialog, messagebox
# Step 1: Create and populate the staff table
conn = sqlite3.connect('myDB.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS staff (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
position TEXT,
office TEXT,
email TEXT
)''')
# Load data from users.json
try:
with open('users.json') as f:
data = json.load(f)
for staff in data:
cursor.execute("INSERT INTO staff (name, position, office, email) VALUES (?, ?, ?, ?)", (staff[0], staff[1], staff[2], staff[3]))
conn.commit()
except Exception as e:
print(f"Data insertion error: {e}")
# Step 2: Function to read staff data from the database
def upload():
cursor.execute('SELECT * FROM staff')
return cursor.fetchall()
# Step 3: GUI setup
root = tk.Tk()
root.title("Staff Management")
listbox = tk.Listbox(root, width=50, height=10)
listbox.pack(pady=10)
# Populate the listbox with staff data
for staff in upload():
listbox.insert(tk.END, f"{staff[1]} - {staff[2]} - {staff[3]} - {staff[4]}")
# Add new staff to the database and listbox
def add_staff():
name = simpledialog.askstring("Input", "Enter name:")
position = simpledialog.askstring("Input", "Enter office:")
office = simpledialog.askstring("Input", "Enter position:")
email = simpledialog.askstring("Input", "Enter email:")
if name and position and office and email:
cursor.execute("INSERT INTO staff (name, position, office, email) VALUES (?, ?, ?, ?)",
(name, position, office, email))
conn.commit()
listbox.insert(tk.END, f"{name} - {position} - {office} - {email}")
else:
messagebox.showwarning("Warning", "All fields are required!")
# Remove selected staff from the database and listbox
def remove_staff():
try:
selected_index = listbox.curselection()[0]
selected_item = listbox.get(selected_index)
name = selected_item.split(' - ')[0]
cursor.execute("DELETE FROM staff WHERE name = ?", (name,))
conn.commit()
listbox.delete(selected_index)
except IndexError:
messagebox.showwarning("Warning", "Please select a staff member to remove!")
add_button = tk.Button(root, text="Add", command=add_staff)
add_button.pack(pady=5)
remove_button = tk.Button(root, text="Remove", command=remove_staff)
remove_button.pack(pady=5)
root.mainloop()
conn.close()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment