Home > Enterprise >  How to Import/Edit Data on GUI without SQL
How to Import/Edit Data on GUI without SQL

Time:09-04

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

CodePudding user response:

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
  • Related