-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-tkinter-mysql.py
154 lines (113 loc) · 4.39 KB
/
python-tkinter-mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from tkinter import *
import tkinter.messagebox as MessageBox
import mysql.connector as mysql
#def hello(): print ('Hello, world')
def insert():
id = e_id.get();
name = e_name.get();
phone = e_phone.get();
bill = e_bill.get();
if(id=="" or name=="" or phone=="" or bill==""):
MessageBox.showinfo("Insert Status", "All Fields are Required!")
else:
con = mysql.connect(host="localhost", user="root", password="", database="python-tkinter")
cursor = con.cursor()
cursor.execute("insert into customer values('"+ id +"','"+ name +"','"+ phone +"','"+ bill +"')")
cursor.execute("commit");
e_id.delete(0, 'end')
e_name.delete(0, 'end')
e_phone.delete(0, 'end')
e_bill.delete(0, 'end')
show()
MessageBox.showinfo("Insert Status", "Inserted Successfully!")
con.close();
def delete():
if(e_id.get() == "" ):
MessageBox.showinfo("Delete Status", "You need to specify ID!")
else:
con = mysql.connect(host="localhost", user="root", password="", database="python-tkinter")
cursor = con.cursor()
cursor.execute("delete from customer where id='"+ e_id.get() +"'")
cursor.execute("commit");
e_id.delete(0, 'end')
e_name.delete(0, 'end')
e_phone.delete(0, 'end')
e_bill.delete(0, 'end')
show()
MessageBox.showinfo("Delete Status", "Deleted Successfully!")
con.close();
def update():
id = e_id.get();
name = e_name.get();
phone = e_phone.get();
bill = e_bill.get();
if(id=="" or name=="" or phone=="" or bill==""):
MessageBox.showinfo("Update Status", "All Fields are Required!")
else:
con = mysql.connect(host="localhost", user="root", password="", database="python-tkinter")
cursor = con.cursor()
cursor.execute("update customer set name='"+ name +"', phone='"+ phone +"', bill='"+ bill +"' where id='"+ id +"'")
cursor.execute("commit");
e_id.delete(0, 'end')
e_name.delete(0, 'end')
e_phone.delete(0, 'end')
e_bill.delete(0, 'end')
show()
MessageBox.showinfo("Update Status", "Updated Successfully!")
con.close();
def get():
if(e_id.get() == "" ):
MessageBox.showinfo("Fetch Status", "You need to specify ID!")
else:
con = mysql.connect(host="localhost", user="root", password="", database="python-tkinter")
cursor = con.cursor()
cursor.execute("select * from customer where id='"+ e_id.get() +"'")
rows = cursor.fetchall()
for row in rows:
e_name.insert(0, row[1])
e_phone.insert(0, row[2])
e_bill.insert(0, row[3])
con.close();
def show():
con = mysql.connect(host="localhost", user="root", password="", database="python-tkinter")
cursor = con.cursor()
cursor.execute("select * from customer")
rows = cursor.fetchall()
list.delete(0, list.size())
for row in rows:
insertData = str(row[0])+' '+ row[1]#+ int(row[2])+' '
list.insert(list.size()+1, insertData)
con.close();
root = Tk()
root.geometry('600x300')
root.title('Customer Records');
id = Label(root, text='Enter ID',font=('bold',10))
id.place(x=20,y=30)
name = Label(root, text='Enter Name',font=('bold',10))
name.place(x=20,y=60)
phone = Label(root, text='Enter Phone',font=('bold',10))
phone.place(x=20,y=90)
bill = Label(root, text='Enter Pending Bills',font=('bold',10))
bill.place(x=20,y=120)
e_id = Entry()
e_id.place(x=150, y=30)
e_name = Entry()
e_name.place(x=150, y=60)
e_phone = Entry()
e_phone.place(x=150, y=90)
e_bill = Entry()
e_bill.place(x=150, y=120)
insert = Button(root, text="Insert", font=("italic", 10), bg="green", command=insert)
insert.place(x=20, y=160)
delete = Button(root, text="Delete", font=("italic", 10), bg="gold", command=delete)
delete.place(x=90, y=160)
update = Button(root, text="Update", font=("italic", 10), bg="red", command=update)
update.place(x=160, y=160)
get = Button(root, text="Get", font=("italic", 10), bg="white", command=get)
get.place(x=240, y=160)
list = Listbox(root)
list.place(x=290, y=30)
show()
#btn = Button(root, text='Hello ', command=hello)
#btn.pack(padx=100, pady=100)
mainloop()