forked from doricardo-zz/crud-tkinter-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
231 lines (213 loc) · 9.44 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# %s - Mysql
# ? -> Sqlite
from tkinter import*
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox
import pymysql
root = Tk()
root.title("Cadastro de Usuários")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = 1280
height = 500
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root.resizable(0, 0)
#==================================METHODS============================================
def Database():
global conn, cursor
conn = pymysql.connect(host = 'doricardo.com', user = 'doric482_admin', password = 'doric482_admin', db = 'doric482_golonger')
cursor = conn.cursor()
def Create():
if FIRSTNAME.get() == "" or LASTNAME.get() == "" or GENDER.get() == "" or ADDRESS.get() == "" or USERNAME.get() == "" or PASSWORD.get() == "":
txt_result.config(text="Please complete the required field!", fg="red")
else:
Database()
#cursor.execute("INSERT INTO `member` (firstname, lastname, gender, address, username, password) VALUES(?, ?, ?, ?, ?, ?)",
cursor.execute("INSERT INTO `member` (firstname, lastname, gender, address, username, password) VALUES(%s, %s, %s, %s, %s, %s)",
(str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), str(ADDRESS.get()), str(USERNAME.get()), str(PASSWORD.get())))
tree.delete(*tree.get_children())
cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
fetch = cursor.fetchall()
for data in fetch:
tree.insert('', 'end', values=(data[0], data[1], data[2], data[3], data[4], data[5], data[6]))
conn.commit()
FIRSTNAME.set("")
LASTNAME.set("")
GENDER.set("")
ADDRESS.set("")
USERNAME.set("")
PASSWORD.set("")
cursor.close()
conn.close()
txt_result.config(text="Created a data!", fg="green")
def Read():
tree.delete(*tree.get_children())
Database()
cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
fetch = cursor.fetchall()
for data in fetch:
tree.insert('', 'end', values=(data[0], data[1], data[2], data[3], data[4], data[5], data[6]))
cursor.close()
conn.close()
txt_result.config(text="Successfully read the data from database", fg="black")
def Update():
Database()
if GENDER.get() == "":
txt_result.config(text="Por favor, informe o sexo", fg="red")
else:
tree.delete(*tree.get_children())
cursor.execute("UPDATE `member` SET `firstname` = %s, `lastname` = %s, `gender` = %s, `address` = %s, `username` = %s, `password` = %s WHERE `mem_id` = %s",
(str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), str(ADDRESS.get()), str(USERNAME.get()), str(PASSWORD.get()), int(mem_id)))
conn.commit()
cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
fetch = cursor.fetchall()
for data in fetch:
tree.insert('', 'end', values=(data[0], data[1], data[2], data[3], data[4], data[5], data[6]))
cursor.close()
conn.close()
FIRSTNAME.set("")
LASTNAME.set("")
GENDER.set("")
ADDRESS.set("")
USERNAME.set("")
PASSWORD.set("")
btn_create.config(state=NORMAL)
btn_read.config(state=NORMAL)
btn_update.config(state=DISABLED)
btn_delete.config(state=NORMAL)
txt_result.config(text="Registro atualizado como sucesso", fg="black")
def OnSelected(event):
global mem_id;
curItem = tree.focus()
contents =(tree.item(curItem))
selecteditem = contents['values']
mem_id = selecteditem[0]
FIRSTNAME.set("")
LASTNAME.set("")
GENDER.set("")
ADDRESS.set("")
USERNAME.set("")
PASSWORD.set("")
FIRSTNAME.set(selecteditem[1])
LASTNAME.set(selecteditem[2])
GENDER.set(selecteditem[3])
ADDRESS.set(selecteditem[4])
USERNAME.set(selecteditem[5])
PASSWORD.set(selecteditem[6])
btn_create.config(state=DISABLED)
btn_read.config(state=DISABLED)
btn_update.config(state=NORMAL)
btn_delete.config(state=DISABLED)
def Delete():
if not tree.selection():
txt_result.config(text="Please select an item first", fg="red")
else:
result = tkMessageBox.askquestion('Cadastro Usuários', 'Você tem certeza que deseja excluir o registro?', icon="warning")
if result == 'yes':
curItem = tree.focus()
contents =(tree.item(curItem))
selecteditem = contents['values']
tree.delete(curItem)
Database()
cursor.execute("DELETE FROM `member` WHERE `mem_id` = %d" % selecteditem[0])
conn.commit()
cursor.close()
conn.close()
txt_result.config(text="Registro excluído como sucesso", fg="black")
def Exit():
result = tkMessageBox.askquestion('Cadastro Usuários', 'Tem certeza que deseja sair?', icon="warning")
if result == 'yes':
root.destroy()
exit()
#==================================VARIABLES==========================================
FIRSTNAME = StringVar()
LASTNAME = StringVar()
GENDER = StringVar()
ADDRESS = StringVar()
USERNAME = StringVar()
PASSWORD = StringVar()
#==================================FRAME==============================================
Top = Frame(root, width=900, height=50, bd=2, relief="raise")
Top.pack(side=TOP)
Left = Frame(root, width=300, height=500, bd=2, relief="raise")
Left.pack(side=LEFT)
Right = Frame(root, width=600, height=500, bd=2, relief="raise")
Right.pack(side=RIGHT)
Forms = Frame(Left, width=300, height=450)
Forms.pack(side=TOP)
Buttons = Frame(Left, width=300, height=100, bd=2, relief="raise")
Buttons.pack(side=BOTTOM)
RadioGroup = Frame(Forms)
Male = Radiobutton(RadioGroup, text="Masculino", variable=GENDER, value="Male", font=('arial', 16)).pack(side=LEFT)
Female = Radiobutton(RadioGroup, text="Feminino", variable=GENDER, value="Female", font=('arial', 16)).pack(side=LEFT)
#==================================LABEL WIDGET=======================================
txt_title = Label(Top, width=900, font=('arial', 24), text = "Cadastro Usuários")
txt_title.pack()
txt_firstname = Label(Forms, text="Nome:", font=('arial', 16), bd=15)
txt_firstname.grid(row=0, sticky="e")
txt_lastname = Label(Forms, text="Sobrenome:", font=('arial', 16), bd=15)
txt_lastname.grid(row=1, sticky="e")
txt_gender = Label(Forms, text="Sexo:", font=('arial', 16), bd=15)
txt_gender.grid(row=2, sticky="e")
txt_address = Label(Forms, text="Endereço:", font=('arial', 16), bd=15)
txt_address.grid(row=3, sticky="e")
txt_username = Label(Forms, text="Usuário:", font=('arial', 16), bd=15)
txt_username.grid(row=4, sticky="e")
txt_password = Label(Forms, text="Senha:", font=('arial', 16), bd=15)
txt_password.grid(row=5, sticky="e")
txt_result = Label(Buttons)
txt_result.pack(side=TOP)
#==================================ENTRY WIDGET=======================================
firstname = Entry(Forms, textvariable=FIRSTNAME, width=30)
firstname.grid(row=0, column=1)
lastname = Entry(Forms, textvariable=LASTNAME, width=30)
lastname.grid(row=1, column=1)
RadioGroup.grid(row=2, column=1)
address = Entry(Forms, textvariable=ADDRESS, width=30)
address.grid(row=3, column=1)
username = Entry(Forms, textvariable=USERNAME, width=30)
username.grid(row=4, column=1)
password = Entry(Forms, textvariable=PASSWORD, show="*", width=30)
password.grid(row=5, column=1)
#==================================BUTTONS WIDGET=====================================
btn_create = Button(Buttons, width=10, text="Create", command=Create)
btn_create.pack(side=LEFT)
btn_read = Button(Buttons, width=10, text="Read", command=Read )
btn_read.pack(side=LEFT)
btn_update = Button(Buttons, width=10, text="Update", command=Update, state=DISABLED)
btn_update.pack(side=LEFT)
btn_delete = Button(Buttons, width=10, text="Delete", command=Delete)
btn_delete.pack(side=LEFT)
btn_exit = Button(Buttons, width=10, text="Exit", command=Exit)
btn_exit.pack(side=LEFT)
#==================================LIST WIDGET========================================
scrollbary = Scrollbar(Right, orient=VERTICAL)
scrollbarx = Scrollbar(Right, orient=HORIZONTAL)
tree = ttk.Treeview(Right, columns=("MemberID", "Firstname", "Lastname", "Gender", "Address", "Username", "Password"), selectmode="extended", height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('MemberID', text="MemberID", anchor=W)
tree.heading('Firstname', text="Nome", anchor=W)
tree.heading('Lastname', text="Sobrenome", anchor=W)
tree.heading('Gender', text="Sexo", anchor=W)
tree.heading('Address', text="Endereço", anchor=W)
tree.heading('Username', text="Usuário", anchor=W)
tree.heading('Password', text="Senha", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=0)
tree.column('#2', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.column('#4', stretch=NO, minwidth=0, width=80)
tree.column('#5', stretch=NO, minwidth=0, width=200)
tree.column('#6', stretch=NO, minwidth=0, width=120)
tree.column('#7', stretch=NO, minwidth=0, width=120)
tree.pack()
tree.bind('<Double-Button-1>', OnSelected)
#==================================INITIALIZATION=====================================
if __name__ == '__main__':
Read()
root.mainloop()