我是 Python 新手,想實作一個簡單的員工管理系統(如附圖所示),它執行以下功能
? 用于輸入、查看和匯出員工資料的 GUI。(我已經完成了這個任務)
? 能夠將資料匯出到 Excel 或 csv 檔案中。(我已經完成了這個任務)
? 能夠將資料從 Excel 或 csv 檔案匯入 GUI。
? 能夠在螢屏上編輯員工資料并將更新的結果保存到檔案中
我需要實作最后兩個任務(即,從 CSV 檔案匯入資料并顯示在 GUI 視窗而不是 Python 控制臺上),因為我當前的代碼將 CSV 檔案內容加載到 Python 控制臺中。
此外,我想在 GUI 螢屏上編輯員工資料并將更新的結果保存到 csv 檔案。
這是我的代碼
from csv import *
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
root = Tk()
root.title("Employee Management System")
root.geometry("700x350")
root.maxsize(width=700, height=350)
root.minsize(width=700, height=350)
root.configure(background="dark gray")
# Define Variables
main_lst=[]
def OpenFile():
filepath=filedialog.askopenfilename()
# print(filepath)
# OR
file=open(filepath,'r')
print(file.read())
file.close
def Add():
lst=[txtFullname.get(),txtAddress.get(),txtAge.get(),txtPhoneNumber.get(),txtGender.get()]
main_lst.append(lst)
messagebox.showinfo("Information","The data has been added successfully")
def Save():
with open("data_entry.csv","w") as file:
Writer=writer(file)
Writer.writerow(["txtFullname","txtAddress","txtAge","txtPhoneNumber","txtGender"])
Writer.writerows(main_lst)
messagebox.showinfo("Information","Saved succesfully")
def Clear():
txtFullname.delete(0,END)
txtAddress.delete(0,END)
txtAge.delete(0,END)
txtPhoneNumber.delete(0,END)
txtGender.delete(0,END)
def Exit():
wayOut = tkinter.messagebox.askyesno("Employee Management System", "Do you want to exit the
system")
if wayOut > 0:
root.destroy()
return
# Label Widget
labelFN = Label( root,text="Full Name", font=('arial', 12, 'bold'), bd=10, fg="white",
bg="dark blue").grid(row=1, column=0)
labelAdd = Label(root, text="Home Address", font=('arial', 12, 'bold'), bd=10, fg="white",
bg="dark blue").grid(row=2, column=0)
labelAge = Label( root,text="Age", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark
blue").grid(row=3, column=0)
labelPhone_Num = Label( root, text="Phone Number", font=('arial', 12, 'bold'), bd=10,
fg="white", bg="dark blue").grid(row=4, column=0)
labelGender = Label( text="Gender", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark
blue").grid(row=5, column=0)
# Entry Widget
txtFullname = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtFullname.grid(row=1, column=1)
txtAddress = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtAddress.grid(row=2, column=1)
txtAge = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtAge.grid(row=3, column=1)
txtPhoneNumber = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtPhoneNumber.grid(row=4, column=1)
txtGender = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtGender.grid(row=5, column=1)
# Buttons
ButtonLoad = Button(text='LoadFile', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'),
width=8, fg="black",bg="dark gray", command=OpenFile).grid(row=6, column=0)
ButtonAdd = Button( text='Add Record', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'),
width=8, fg="black", bg="dark gray", command=Add).grid(row=6, column=1)
ButtonSave = Button(text='Save', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8,
fg="black", bg="dark gray", command=Save).grid(row=6, column=2)
ButtonClear = Button(text='Clear', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8,
fg="black", bg="dark gray", command=Clear).grid(row=6, column=3)
ButtonExit = Button(text='Exit', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8,
fg="black",bg="dark gray", command=Exit).grid(row=6, column=4)
'''
ButtonImport = Button(text='Import', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'),
width=8, fg="black",bg="dark gray", command=Import).grid(row=6, column=5)
'''
root.mainloop()
員工管理系統-當前輸出截圖
uj5u.com熱心網友回復:
為了從 csv 檔案匯入資料,您可以使用 csv 模塊中的方法,并通過您的函式reader將該資訊加載到您的main_lst串列中。OpenFile我不確定是否要編輯,因為您的 UI 一次只能顯示一個條目,因此很難知道您要編輯哪個條目。
from csv import *
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
root = Tk()
root.title("Employee Management System")
root.geometry("700x350")
root.maxsize(width=700, height=350)
root.minsize(width=700, height=350)
root.configure(background="dark gray")
# Define Variables
main_lst=[]
def OpenFile():
filepath=filedialog.askopenfilename()
with open(filepath, "rt") as csvfile:
rows = reader(csvfile)
for row in rows:
main_lst.append(row)
lst = [txtFullname, txtAddress, txtAge, txtPhoneNumber, txtGender]
for i,x in enumerate(lst):
x.insert(0, row[i])
def Add():
lst=[txtFullname.get(),txtAddress.get(),txtAge.get(),txtPhoneNumber.get(),txtGender.get()]
main_lst.append(lst)
messagebox.showinfo("Information","The data has been added successfully")
def Save():
with open("data_entry.csv","w") as file:
Writer=writer(file)
Writer.writerow(["txtFullname","txtAddress","txtAge","txtPhoneNumber","txtGender"])
Writer.writerows(main_lst)
messagebox.showinfo("Information","Saved succesfully")
def Clear():
txtFullname.delete(0,END)
txtAddress.delete(0,END)
txtAge.delete(0,END)
txtPhoneNumber.delete(0,END)
txtGender.delete(0,END)
def Exit():
wayOut = messagebox.askyesno("Employee Management System", "Do you want to exit the system")
if wayOut > 0:
root.destroy()
return
# Label Widget
labelFN = Label( root,text="Full Name", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark blue").grid(row=1, column=0)
labelAdd = Label(root, text="Home Address", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark blue").grid(row=2, column=0)
labelAge = Label( root,text="Age", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark blue").grid(row=3, column=0)
labelPhone_Num = Label( root, text="Phone Number", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark blue").grid(row=4, column=0)
labelGender = Label( text="Gender", font=('arial', 12, 'bold'), bd=10, fg="white", bg="dark blue").grid(row=5, column=0)
# Entry Widget
txtFullname = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtFullname.grid(row=1, column=1)
txtAddress = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtAddress.grid(row=2, column=1)
txtAge = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtAge.grid(row=3, column=1)
txtPhoneNumber = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtPhoneNumber.grid(row=4, column=1)
txtGender = Entry(root, font=('arial', 12, 'bold'), bd=4, width=22, justify='left')
txtGender.grid(row=5, column=1)
# Buttons
ButtonLoad = Button(text='LoadFile', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8, fg="black",bg="dark gray", command=OpenFile).grid(row=6, column=0)
ButtonAdd = Button( text='Add Record', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8, fg="black", bg="dark gray", command=Add).grid(row=6, column=1)
ButtonSave = Button(text='Save', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8, fg="black", bg="dark gray", command=Save).grid(row=6, column=2)
ButtonClear = Button(text='Clear', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8, fg="black", bg="dark gray", command=Clear).grid(row=6, column=3)
ButtonExit = Button(text='Exit', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'), width=8, fg="black",bg="dark gray", command=Exit).grid(row=6, column=4)
'''
ButtonImport = Button(text='Import', padx=10, pady=8, bd=4, font=('arial', 12, 'bold'),
width=8, fg="black",bg="dark gray", command=Import).grid(row=6, column=5)
'''
root.mainloop()
您的 csv 檔案應如下所示:
例子.csv
txtFullname,txtAddress,txtAge,txtPhoneNumber,txtGender
Bob,Main St.,35,18004438768,Male
Alice,1st St.,62,922-333-1253,Female
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504273.html
上一篇:如何在標簽2上實時更新和顯示用戶在ENTRYlabel1中寫的內容?
下一篇:滾動條無法在畫布上正常作業
