Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Lab Advanced Software Development
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bui2.Huan@live.uwe.ac.uk
Lab Advanced Software Development
Commits
8b1bef69
Commit
8b1bef69
authored
2 months ago
by
Bui2.Huan@live.uwe.ac.uk
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
888c9b6f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Week 10/activity4.py
+79
-0
79 additions, 0 deletions
Week 10/activity4.py
with
79 additions
and
0 deletions
Week 10/activity4.py
0 → 100644
+
79
−
0
View file @
8b1bef69
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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment