我是新手,想要实现一个简单的员工管理系统(如所附照片所示),它具有以下功能

·用于输入、查看和导出员工数据的图形用户界面.(Already completed)

·能够将数据导出到Excel或CSV文件.(Already completed but having an issue-Explained bellow)

·能够将所有数据从Excel或CSV文件导入到图形用户界面.

以下是图形用户界面的一个示例

enter image description here

这是CSV文件

    Fullname            Age Gender  Position              Phone Number  Address
    Ali Talib           59  Male    Vice-Chancellor       1752555555    UK-London
    Afaf Neamah         23  Female  Manager               7912394404    UK-Plymouth
    Hussein Neamah      22  Male    Head of Department    7335952523    UK-London
    Estabraq Aldalboos  33  Female  Engineer              7575252324    UK-Plymouth
    Nathan Clarke       45  Male    Deputy Head of School 7916682090    UK-London
    Neamah AL-Naffakh   37  Male    Lecturer              7817792202    UK-Plymouth

I need to develop the code to do the following

·目前有I am人遇到了问题.每次实现后,代码都会覆盖CSV文件.例如,假设我在CSV文件上有10条记录.一旦我关闭或退出Python,并再次运行系统,它将存储最新的记录并删除所有旧记录(用旧日期覆盖新输入)

·I need更好地从CSV导入所有数据并将其放入列表或文本框(或任何建议).

Here is the code

import csv 
from csv import *
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox


window=Tk()                                               #1-1
window.geometry("800x500+0+0")                                #1-2
window.title("Employee Management System")
window.maxsize(width=800,height=500)
window.minsize(width=800,height=500)
main_lst=[]

# add the column value into the list 

FilePath = '/Users/nhal-naffakh/Desktop/Desktop/Sec-Interview/Data.csv'
# Create file object that open the file located in FilePath and store the file info in the object
File=open(FilePath)
# Create Object that read the csv file
# Convert each row in the CSV file into list of string and store it in Object
Reader=csv.reader(File) 
Data=list(Reader)
del(Data[0])                                             #1-5 Delete title of the first row in the CSV

#print(Data[5][1])

# Create set of Functions 
def view():                                              #1-8
  index=listbox1.curselection()[0]
  NameLabel2.config(text=Data[index][0])
  AgeLabel2.config(text=Data[index][1])
  GenderLabel2.config(text=Data[index][2])
  PositionLabel2.config(text=Data[index][3])
  AddressLabel2.config(text=Data[index][4])
  return None

def OpenFile():
    filepath=filedialog.askopenfilename()
#   print(filepath)
# OR
    file=open(filepath,'r')
    print(file.read()) 
    file.close
    
def Add():
   lst=[Nametxt.get(),Agetxt.get(),Gendertxt.get(),Positiontxt.get(),Numbertxt.get(),Addresstxt.get()]
   main_lst.append(lst)
   messagebox.showinfo("Information","The data has been added successfully")
   
def Save():
   with open("Data.csv","w") as file:
      Writer=writer(file)
      Writer.writerow(["Fullname","Age","Gender","Position","Phone Number","Address"])
      Writer.writerows(main_lst)
      messagebox.showinfo("Information","Saved succesfully")
            
def Clear():
   Nametxt.delete(0,END)
   Agetxt.delete(0,END)
   Gendertxt.delete(0,END)
   Positiontxt.delete(0,END)
   Numbertxt.delete(0,END)
   Addresstxt.delete(0,END)

def Exit():
    wayOut = tkinter.messagebox.askyesno("Employee Management System", "Do you want to exit the system")
    if wayOut > 0:
        window.destroy()
        return
   
# extract entire column into List to show it on GUI later 
List_of_Name=[]
for x in list(range(0,len(Data))):
    #print(Data[x][0])
    List_of_Name.append(Data[x][0])    
    var=StringVar(value=List_of_Name)                   #1-3
    listbox1=Listbox(window,listvariable=var) #1-4 Modified by adding var
    listbox1.grid(row=3,column=0)
    
    buttonView=Button(text="ViewRecord",padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                      width=8,fg='black',bg="dark gray",command=view).grid(row=3,column=1)                     #1-7
     
    # Label Widget
    NameLabel=Label(window,text="FullName").grid(row=5,column=0,sticky="w")        #1-9
    AgeLabel=Label(window,text="Age").grid(row=6,column=0,sticky="w")
    GenderLabel=Label(window,text="Gender").grid(row=7,column=0,sticky="w")
    PositionLabel=Label(window,text="Position").grid(row=8,column=0,sticky="w")
    AddressLabel=Label(window,text="Address").grid(row=9,column=0,sticky="w")
    
    
    NameLabel2=Label(window,text="")        #1-10
    NameLabel2.grid(row=5,column=1,sticky="w")
    AgeLabel2=Label(window,text="")
    AgeLabel2.grid(row=6,column=1,sticky="w")
    GenderLabel2=Label(window,text="")
    GenderLabel2.grid(row=7,column=1,sticky="w")
    PositionLabel2=Label(window,text="")
    PositionLabel2.grid(row=8,column=1,sticky="w")
    AddressLabel2=Label(window,text="")
    AddressLabel2.grid(row=9,column=1,sticky="w")

    # Label Widget & Entry Widget    #1-10
    Namelabel3=Label(window,text="Full Name",font=('arial',12,'bold'),bd=3,fg="white",
                     bg="dark blue",).grid(row=0,column=0,sticky="w")
    Nametxt=Entry(window,font=('ariel',12),bd=4,width=22, justify='left')
    Nametxt.grid(row=0,column=1)
    
    Agelabel3=Label(window,text="Age",font=("ariel",12,'bold'),bd=3,fg='white',
                    bg="dark blue",).grid(row=0,column=2)
    Agetxt=Entry(window,font=('ariel',12),bd=4, justify='left')
    Agetxt.grid(row=0,column=3)
    
    GenderLabel3=Label(window,text="Gender",font=('ariel',12,'bold'),bd=3,fg='white',
                    bg="dark blue").grid(row=1,column=0,sticky="w")
    Gendertxt=Entry(window,font=('ariel',12),bd=4, justify='left')
    Gendertxt.grid(row=1,column=1)
    
    PositionLabel3=Label(window,text="Position",font=('ariel',12,'bold'),bd=3,fg='white',
                    bg="dark blue").grid(row=1,column=2,sticky="w")
    Positiontxt=Entry(window,font=('ariel',12),bd=4, justify='left')
    Positiontxt.grid(row=1,column=3)
    
    NumberLabel3=Label(window,text="Mob Number",font=('ariel',12,'bold'),bd=3,fg='white',
                    bg="dark blue").grid(row=2,column=0,sticky="w")
    Numbertxt=Entry(window,font=('ariel',12),bd=4, justify='left')
    Numbertxt.grid(row=2,column=1)
    
    AddressLabel3=Label(window,text="Address",font=('ariel',12,'bold'),bd=3,fg='white',
                    bg="dark blue").grid(row=2,column=2,sticky="w")
    Addresstxt=Entry(window,font=('ariel',12),bd=4, justify='left')
    Addresstxt.grid(row=2,column=3)

    #  Button
    LoadButton=Button(text="Load File", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                      width=8,fg='black',bg="dark gray", command=OpenFile).grid(row=3,column=2)
    AddButton=Button(text="Add Record", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                     width=8,fg='black',bg="dark gray",command=Add).grid(row=3,column=3)
    SaveButton=Button(text="Save", padx=10, pady=4, bd=4, font=('ariel',12,'bold'), 
                      width=8,fg='black',bg="dark gray",command=Save).grid(row=4,column=1)
    ClearButton=Button(text="Clear", padx=10, pady=4, bd=4, font=('ariel',12,'bold'), 
                       width=8,fg='black',bg="dark gray",command=Clear).grid(row=4,column=2)
    ExitButton=Button(text="Exit", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                      width=8,fg='black', bg="dark gray",command=Exit).grid(row=4,column=3)

window.mainloop()

推荐答案

问题是,您的信息存储在程序启动时加载的Data变量中,然后您有main_list,当您添加新条目时,它将被填充.退出或保存程序时,只会将main_list中的内容保存到csv文件中,并完全忽略Data变量的内容,从而删除启动程序之前列表中的任何内容.

这可以通过简单地将两个列表合并成一个由程序开始时CSV文件中的内容填充的单个main_list并通过在程序执行期间添加新记录来容易地解决.您还需要在添加新记录时将新名称存储在列表框1中,方法是在Add函数中将文本插入列表框本身

例如:

我做了一些内联笔记,并修复了几个缩进问题.

import csv
from tkinter import *
from tkinter import messagebox

def load_data(path):      # Load data from csv file at program start.
    reader = csv.reader(open(path))
    return list(reader)[1:]

window=Tk()
window.geometry("800x500+0+0")
window.title("Employee Management System")
window.maxsize(width=800,height=500)
window.minsize(width=800,height=500)
FILEPATH = '/Users/nhal-naffakh/Desktop/Desktop/Sec-Interview/Data.csv'

main_lst = load_data(FILEPATH)  # Load csv data into `main_lst`


def view():
  index = listbox1.curselection()[0]
  NameLabel2.config(text=main_lst[index][0])
  AgeLabel2.config(text=main_lst[index][1])
  GenderLabel2.config(text=main_lst[index][2])
  PositionLabel2.config(text=main_lst[index][3])
  AddressLabel2.config(text=main_lst[index][4])

def OpenFile():
    pass

def Add():
   name = Nametxt.get()  # get the name of new entry
   lst = [name, Agetxt.get(), Gendertxt.get(), Positiontxt.get(), Numbertxt.get(), Addresstxt.get()]
   main_lst.append(lst)
   listbox1.insert(len(List_of_Name), name)  # add it to the listbox list
   List_of_Name.append(name)  # store it in the list of names
   messagebox.showinfo("Information","The data has been added successfully")

def Save():
   with open(FILEPATH,"w") as file:
      writer = csv.writer(file, lineterminator='\n')
      writer.writerow(["Fullname","Age","Gender","Position","Phone Number","Address"])
      writer.writerows(main_lst)  #  now when it writes to file it will contain all the data.
      messagebox.showinfo("Information","Saved succesfully")

def Clear():
   Nametxt.delete(0,END)
   Agetxt.delete(0,END)
   Gendertxt.delete(0,END)
   Positiontxt.delete(0,END)
   Numbertxt.delete(0,END)
   Addresstxt.delete(0,END)

def Exit():
    wayOut = messagebox.askyesno("Employee Management System", "Do you want to exit the system")
    if wayOut > 0:
        Save()  # Added a call to save upon exiting.
        window.destroy()
        return

List_of_Name=[]
for x in list(range(0,len(main_lst))):
    List_of_Name.append(main_lst[x][0])

var = StringVar(value=List_of_Name)
listbox1 = Listbox(window, listvariable=var)
listbox1.grid(row=3,column=0)

buttonView = Button(text="ViewRecord", padx=10, pady=4, bd=4,
                    font=('ariel',12,'bold'), width=8, fg='black',
                    bg="dark gray", command=view).grid(row=3,column=1)
NameLabel = Label(window,text="FullName").grid(row=5,column=0,sticky="w")
AgeLabel=Label(window,text="Age").grid(row=6,column=0,sticky="w")
GenderLabel=Label(window,text="Gender").grid(row=7,column=0,sticky="w")
PositionLabel=Label(window,text="Position").grid(row=8,column=0,sticky="w")
AddressLabel=Label(window,text="Address").grid(row=9,column=0,sticky="w")
NameLabel2=Label(window,text="")
NameLabel2.grid(row=5,column=1,sticky="w")
AgeLabel2=Label(window,text="")
AgeLabel2.grid(row=6,column=1,sticky="w")
GenderLabel2=Label(window,text="")
GenderLabel2.grid(row=7,column=1,sticky="w")
PositionLabel2=Label(window,text="")
PositionLabel2.grid(row=8,column=1,sticky="w")
AddressLabel2=Label(window,text="")
AddressLabel2.grid(row=9,column=1,sticky="w")
Namelabel3=Label(window,text="Full Name",
                    font=('arial',12,'bold'),bd=3,fg="white",
                    bg="dark blue",).grid(row=0,column=0,sticky="w")
Nametxt = Entry(window,font=('ariel',12),bd=4,width=22, justify='left')
Nametxt.grid(row=0,column=1)

Agelabel3=Label(window,text="Age",font=("ariel",12,'bold'),bd=3,fg='white',
                bg="dark blue",).grid(row=0,column=2)
Agetxt=Entry(window,font=('ariel',12),bd=4, justify='left')
Agetxt.grid(row=0,column=3)

GenderLabel3=Label(window,text="Gender",font=('ariel',12,'bold'),bd=3,fg='white',
                bg="dark blue").grid(row=1,column=0,sticky="w")
Gendertxt=Entry(window,font=('ariel',12),bd=4, justify='left')
Gendertxt.grid(row=1,column=1)

PositionLabel3=Label(window,text="Position",font=('ariel',12,'bold'),bd=3,fg='white',
                bg="dark blue").grid(row=1,column=2,sticky="w")
Positiontxt=Entry(window,font=('ariel',12),bd=4, justify='left')
Positiontxt.grid(row=1,column=3)

NumberLabel3=Label(window,text="Mob Number",font=('ariel',12,'bold'),bd=3,fg='white',
                bg="dark blue").grid(row=2,column=0,sticky="w")
Numbertxt=Entry(window,font=('ariel',12),bd=4, justify='left')
Numbertxt.grid(row=2,column=1)

AddressLabel3=Label(window,text="Address",font=('ariel',12,'bold'),bd=3,fg='white',
                bg="dark blue").grid(row=2,column=2,sticky="w")
Addresstxt=Entry(window,font=('ariel',12),bd=4, justify='left')
Addresstxt.grid(row=2,column=3)

#  Button
LoadButton=Button(text="Load File", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                    width=8,fg='black',bg="dark gray", command=OpenFile).grid(row=3,column=2)
AddButton=Button(text="Add Record", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                    width=8,fg='black',bg="dark gray",command=Add).grid(row=3,column=3)
SaveButton=Button(text="Save", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                    width=8,fg='black',bg="dark gray",command=Save).grid(row=4,column=1)
ClearButton=Button(text="Clear", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                    width=8,fg='black',bg="dark gray",command=Clear).grid(row=4,column=2)
ExitButton=Button(text="Exit", padx=10, pady=4, bd=4, font=('ariel',12,'bold'),
                    width=8,fg='black', bg="dark gray",command=Exit).grid(row=4,column=3)

window.mainloop()

Python相关问答推荐

将数组操作转化为纯numpy方法

Pandas 密集排名具有相同值,按顺序排列

只需使用Python在图像中保留 colored颜色 范围区域

合并其中一个具有重叠范围的两个框架的最佳方法是什么?

如何观察cv2.erode()的中间过程?

删除pandas rame时间序列列中未更改的值

NumPy中的右矩阵划分,还有比NP.linalg.inv()更好的方法吗?

无法使用equals_html从网址获取全文

仿制药的类型铸造

try 在树叶 map 上应用覆盖磁贴

用Python解密Java加密文件

大小为M的第N位_计数(或人口计数)的公式

如何使用pytest来查看Python中是否存在class attribution属性?

try 将一行连接到Tensorflow中的矩阵

无法在Docker内部运行Python的Matlab SDK模块,但本地没有问题

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

如何更改groupby作用域以找到满足掩码条件的第一个值?

使用BeautifulSoup抓取所有链接

在Python中调用变量(特别是Tkinter)

基于Scipy插值法的三次样条系数