Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

from tkinter import*

import tkinter as tk
import mysql.connector as mysql
from tkinter import ttk,messagebox

#Connect to database

db = mysql.connect(host="localhost",user="root", password="root",
database="yourschema")
mycursor = db.cursor()

#define the button function

def Add():
id = e1.get()
name = e2.get()
email = e3.get()

sql = "INSERT INTO yourtablename (idnumber,name,email) VALUES(%s,%s,%s)


value = (id,name,email)
mycursor.execute(sql, value)
db.commit()

def Edit():
id = e1.get()
name = e2.get()
email = e3.get()

sql = "UPDATE yourtablename SET name=%s,email=%s where idnumber = %s"


value = (name,email,id)
mycursor.execute(sql, value)
db.commit()

def Delete():
id = e1.get()

sql = "DELETE FROM yourtablename WHERE idnumber=%s"


value = (id)
mycursor.execute(sql, value)
db.commit()

def show():
mycursor.execute("SELECT * FROM yourtablename")
records = mycursor.fetchall()
print(record)

for i,(idnumber,name,email) in enumerate(records, start=1):


listbox.insert("","end",values=(idnumber,name,email))

root =Tk()
root.geometry("800x800")

#Label1

Label(root, text="Data Collect", font=('Times New


Roman',30,'bold)).grid(row=0,column=3)
idnumber = Label(root,text"Id Number"
name = Label(root,text"Name"
email = Label(root,text"Email"

idnumber.grid(row(row=1,column=2)
name.grid(row=2,column=2)
email.grid(row=2,column=2)

e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)

e1.grid(row=1,column=3)
e2.grid(row=2,column=3)
e3.grid(row=3,column=3)

Button(root, text="Add", command=Add, height=3,width=10).place(x=100,y=450)


Button(root, text="Edit", command=Edit, height=3,width=10).place(x=200,y=450)
Button(root, text="Delete", command=Delete, height=3,width=10).place(x=300,y=450)
Button(root, text="Show List", command=show, height=3,width=10).place(x=400,y=450)

#listbox for record database

column1 = ('Idnumber','name','Email')
listbox = ttk.Treeview(root,columns=column1,show='heading')

for col in column1:


listbox.heading(col,text=col)
listbox.grid(row=1,column=0,columnspan=2)
listbox.place(x=9, y=150)

root.mainloop()

You might also like