2

I am new to Python and would like to implement a simple Employee Management System (as shown on the attached photo) that do the following functionality

• A GUI for entering, viewing, and exporting employee data. (I have done this task)

• The ability to export data into an Excel or csv file. (I have done this task)

• The ability to import data from an Excel or csv file into the GUI.

• The ability to edit employee data on screen and save the updated result to a file

I need to implement the last two tasks (i.e., import data from an CSV file and display on the GUI Window rather than on the Python Console) as my current code load the CSV file content into the Python console .

Moreover, I would like to edit the employee data on the GUI screen and save the updated result to the csv file.

Here is my code

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()

Employee Management System-Screenshot of the current output

Alexander
  • 16,091
  • 5
  • 13
  • 29
Neamah
  • 57
  • 5
  • quick note. replace `from csv import *` with `import csv`. it is clearer that way. – D.L Sep 02 '22 at 23:06

1 Answers1

1

In order to import data from a csv file you can use the reader method from the csv module and simply load that information into your main_lst list through your OpenFile function. I am not sure about editing though since your UI can only show one entry at a time, it makes it difficult to know which entry you are trying to edit.

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()

your csv file should look like this:

example.csv

txtFullname,txtAddress,txtAge,txtPhoneNumber,txtGender
Bob,Main St.,35,18004438768,Male
Alice,1st St.,62,922-333-1253,Female
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • 1
    Dear @Alexander, – Thank you for your reply but its doesnt solve the problem. I want to load data from csv file and display on the GUI window (not on the console) – Neamah Sep 03 '22 at 10:17
  • @Neamah Where do you get the idea that it is showing in the console? Did you even run the code? The code populates each of the input boxes with the last entry from the csv file – Alexander Sep 03 '22 at 10:20
  • 1
    @Neamah So you are receiving a unicode error. That has nothing to do with my code. Also my example code doesn't have any print statements in it, so you clearly aren't running it. – Alexander Sep 03 '22 at 10:36
  • 1
    Ok thanks. But just to let you know this is the output. I run it several times Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\nhal-naffakh\AppData\Local\Programs\Spyder\pkgs\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "c:\users\nhal-naffakh\desktop\desktop\sec-interview\sql.py", line 25, in OpenFile x.insert(0, row[i]) IndexError: list index out of range – Neamah Sep 03 '22 at 10:46
  • okay so that means you have too few or too many fields in your csv file.... there should be at least one for every input box in your program... See the edit to my answer – Alexander Sep 03 '22 at 11:10
  • 1
    Dear @Alexander, I have modified the code little bit and posted another question. Are you able to help me please https://stackoverflow.com/questions/73603096/how-to-do-input-validation-checks – Neamah Sep 04 '22 at 22:02