0% found this document useful (0 votes)
1 views

Tree View

The document is a Python script that creates a data entry form using the Tkinter library. It allows users to add, update, search, and delete entries in a CSV file, as well as generate a PDF report of the data. The script includes functions for handling user input and managing data storage in a structured manner.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Tree View

The document is a Python script that creates a data entry form using the Tkinter library. It allows users to add, update, search, and delete entries in a CSV file, as well as generate a PDF report of the data. The script includes functions for handling user input and managing data storage in a structured manner.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#form_entry.

py
import tkinter #Memanggil framework tkinter
from tkinter import * #Mengimport semua fungsi/class/procedure
from tkinter import messagebox #yang ada di tkinter
import os
import csv
from fpdf import FPDF

#Mendeklarasikan variabel global


global Id_Entri,Nama_Entri,Alamat_Entri,Telp_Entri

csv_filename = 'data.csv'

def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')

Layar=Tk() #Membuat jendela/window bernama 'Layar'


Layar.title("Form Data Entry") #Judul windows
Layar.geometry("600x300") # Mengatur dimensi/ukuran layar (pixel(x,y))

def tambah():
Vid = Id_Entri.get() #Menyimpan entry Id ke variabel Vid
Vnama = Nama_Entri.get()
Valamat = Alamat_Entri.get()
Vtelp = Telp_Entri.get()

vData = open('data.csv','a')
data = csv.writer(vData,lineterminator='\n')
data.writerow([Vid,Vnama,Valamat,Vtelp])
print(vData)
vData.close()
pesan=('Data anda sudah tersimpan!')
print(pesan)
clear_entry()

def clear_entry():
#Menghapus isi kolom entry setelah data tersimpan
Id_Entri.delete(0,END)
Nama_Entri.delete(0,END)
Alamat_Entri.delete(0,END)
Telp_Entri.delete(0,END)
Id_Entri.focus_set() # Menempatkan kembali kursor ke kolom entry Id

def update():
data1 =[]
with open ('data.csv','r') as csv_file :
csv_reader=csv.reader(csv_file, delimiter=",") #, delimiter=","
for row in csv_reader:
data1.append(row)

Headers = data1.pop(0)

data1_sorted = sorted(data1, key=lambda x: int(x[0]))

print("-" * 56)
print(f'{Headers[0]:<5} {Headers[1]:<20} {Headers[2]:<20} {Headers[3]:<15}')
print("-" * 56)
for list in data1_sorted: # Menggunakan data1_sorted yang sudah diurutkan
print(f'{list[0]:<5} {list[1]:<20} {list[2]:<20} {list[3]:<15}')
print("-" * 56)
Id_Entri.focus_set()

def hapus():
#Id_Entri=""
Vid = Id_Entri.get() #Menyimpan entry Id ke variabel Vid
Vnama = Nama_Entri.get()
Valamat = Alamat_Entri.get()
Vtelp = Telp_Entri.get()
Vdelete = [Vid, Vnama,Valamat,Vtelp]
print(Vdelete)

contact1 = []
with open(csv_filename, mode="r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
contact1.append(row)
print(contact1)

contact1.remove(['006', 'Umar wira', 'Jawa barat', '098765'])


print(Vdelete)
print(contact1)

def cari():
Vid = Id_Entri.get()
Vnama = Nama_Entri.get()
contacts = []
with open(csv_filename, mode="r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
contacts.append(row)

#no = input("Cari berdasarkan nomer urut> ")


data_found = []

# mencari contact
indeks = 0
for data in contacts:
if (data['NO'] == Vid) or (data['NAMA'] == Vnama): # no
data_found = contacts[indeks]

indeks += 1

if len(data_found) > 0:
print("DATA DITEMUKAN: ")
print(f"No Id: {data_found['NO']}")
print(f"Nama: {data_found['NAMA']}")
print(f"Alamat: {data_found['ALAMAT']}")
print(f"Telepon: {data_found['TELEPON']}")
print('-'*35)
clear_entry()
Id_Entri.insert(0, data_found['NO'])
Nama_Entri.insert(0, data_found['NAMA'])
Alamat_Entri.insert(0, data_found['ALAMAT'])
Telp_Entri.insert(0, data_found['TELEPON'])
else:
print("Tidak ada data ditemukan")
print('-'*35)
#Menampilakn messagebox->syntax: messagebox.showinfo(title,msg)
messagebox.showinfo('INFO','Tidak ada data ditemukan')

def pdf():
from fpdf import FPDF

pdf = FPDF() # save FPDF() class into a variable pdf


pdf.add_page() # Add a page
pdf.set_font("Arial",size=12) #mengatur jenis & ukuran font

# open the text file in read mode


f = open('E:/LENOVO/Python/Csv/Latihan/data.csv', "r")

# insert the texts in pdf


for x in f:
pdf.cell(200, 10, txt=x, ln=1, align='L')

# save the pdf with name .pdf


pdf.output("data.pdf")
print("File pdf sudah tersedia")
f.close()

#Membuat menu label entry


#1.Menampilkan label "nama_label" dengan ukuran font=12,Koordinat x,y
Label(Layar, text="Id ", font=(None, 12)).place(x=15, y=30)
Label(Layar, text="Nama ", font=(None, 12)).place(x=15, y=60)
#Label(Layar, text="Panggilan :", font=(None, 12)).place(x=600, y=60)
Label(Layar, text="Alamat ", font=(None, 12)).place(x=15, y=90)
Label(Layar, text="Telp/WA ", font=(None, 12)).place(x=15, y=120)

#Membuat Kolom entry:


Id_Entri = Entry(Layar, font=(None, 12)) #
Id_Entri.place(x=160, y=30, width=100) #variabel.place(x=lokasiX,
Nama_Entri = Entry(Layar, font=(None, 12)) #y=lokasiY,
width=lebar(default150))
Nama_Entri.place(x=160, y=60, width=400)
#Panggilan_Entri = Entry(Layar, font=(None, 12))
#Panggilan_Entri.place(x=690, y=60)
Alamat_Entri = Entry(Layar, font=(None, 12))
Alamat_Entri.place(x=160, y=90, width=400) #width=400
Telp_Entri = Entry(Layar, font=(None, 12))
Telp_Entri.place(x=160, y=120)
Id_Entri.focus_set()

#Membuat Button
Button(Layar, text="Tambah", command=tambah, font=(None, 12), height=1,
width=8).place(x=15, y=180)
Button(Layar, text="Update", command=update, font=(None, 12), height=1,
width=8).place(x=110, y=180)
Button(Layar, text="Cari", command=cari, font=(None, 12), height=1,
width=8).place(x=202, y=180)
Button(Layar, text="PDF", command=pdf, font=(None, 12), height=1,
width=8).place(x=294, y=180)
Button(Layar, text="Clear", command=clear_entry, font=(None, 12), height=1,
width=8).place(x=386, y=180)
Button(Layar, text="Quit", command=Layar.destroy, font=(None, 12), height=1,
width=8).place(x=478, y=180)
#Button(Layar,text="Delete",command=hapus,font=(None,12),height=1,width=8).place(x=
247,y=220)

Layar.mainloop() #Penutup Looping/mengakhiri jendela 'Layar'

You might also like