从文件window.py通过tambah_update_button
添加数据时遇到问题,当我单击"Tambah atau Update"
按钮时,我没有从self.id_text
获取值ID,请说明原因?
但是如果我在insertData.py文件中调用Window类,例如Window()
,则程序可以从self.id_text.get()
中获取值
文件window.py
import tkinter as tk
import insertData
root = tk.Tk()
tambah_update_button = tk.Button(root, text="Insert or Update", command=insertData.Window)
tambah_update_button.pack()
root.mainloop()
文件insertData.py
import mysql.connector
import tkinter as tk
import cv2 as cv
class Data:
def __init__(self):
self.conn = mysql.connector.connect(host="localhost", user="root", passwd="", database="facebase")
self.cursor = self.conn.cursor()
self.conn.commit()
def insertOrUpdate(self, Id, Nama, Nim, Jurusan):
query = "SELECT * FROM mhs WHERE id='%s'" % (Id)
self.cursor.execute(query)
print(query)
isRecordExist = 0
for row in self.cursor:
isRecordExist = 1
if (isRecordExist == 1):
query = "UPDATE mhs SET nama='%s', nim='%s', jurusan='%s' WHERE id='%s'" % (Nama, Nim, Jurusan, Id)
else:
query = "INSERT INTO mhs (id,nama,nim,jurusan) VALUES ('%s','%s','%s','%s')" % (Id, Nama, Nim, Jurusan)
self.cursor.execute(query)
self.conn.commit()
db = Data()
class Window:
def __init__(self):
self.window = tk.Tk()
self.window.title('Tambah Data')
self.l1 = tk.Label(self.window, text="ID")
self.l1.config(width=10)
self.l1.grid(row=0, column=0)
self.l2 = tk.Label(self.window, text="Nama")
self.l2.config(width=10)
self.l2.grid(row=1, column=0)
self.l3 = tk.Label(self.window, text="Nim")
self.l3.config(width=10)
self.l3.grid(row=2, column=0)
self.l4 = tk.Label(self.window, text="Jurusan")
self.l4.config(width=10)
self.l4.grid(row=3, column=0)
self.id_text = tk.StringVar()
self.e1 = tk.Entry(self.window, textvariable=self.id_text)
self.e1.config(width=30)
self.e1.grid(row=0, column=1)
self.nama_text = tk.StringVar()
self.e2 = tk.Entry(self.window, textvariable=self.nama_text)
self.e2.config(width=30)
self.e2.grid(row=1, column=1)
self.nim_text = tk.StringVar()
self.e3 = tk.Entry(self.window, textvariable=self.nim_text)
self.e3.config(width=30)
self.e3.grid(row=2, column=1)
self.jurusan_text = tk.StringVar()
self.e4 = tk.Entry(self.window, textvariable=self.jurusan_text)
self.e4.config(width=30)
self.e4.grid(row=3, column=1)
self.b1 = tk.Button(self.window, text="Tambah atau Update", command=self.tambah_update_command)
self.b1.grid(row=4, columnspan=2)
self.window.mainloop()
def dataSet(self):
wajah = cv.CascadeClassifier('classifiers/face-detect.xml')
cam = cv.VideoCapture(0)
id = str(self.id_text.get())
# nama = str(self.nama_text.get())
sampleNum = 0
while True:
_, img = cam.read()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
wajahs = wajah.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in wajahs:
sampleNum = sampleNum + 1
cv.imwrite("dataSet/User." + str(id) + "." + str(sampleNum) + ".jpg",
cv.resize(gray[y:y + h, x:x + w], (200, 200)))
cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv.imshow('DataSet', img)
# cv.imshow('Gray', gray)
cv.waitKey(1)
if (sampleNum >= 20):
break
cam.release()
cv.destroyAllWindows()
def tambah_update_command(self):
db.insertOrUpdate(self.id_text.get(), self.nama_text.get(), self.nim_text.get(), self.jurusan_text.get())
self.window.destroy()
self.dataSet()
这是我得到的错误:
Exception in Tkinter callback
SELECT * FROM mhs WHERE id=''
Traceback (most recent call last):
File "D:\Program\Python36\lib\site-packages\mysql\connector\connection_cext.py", line 395, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Incorrect integer value: '' for column 'id' at row 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Program\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "D:\py\skripsi\insertData.py", line 102, in tambah_update_command
db.insertOrUpdate(self.id_text.get(), self.nama_text.get(), self.nim_text.get(), self.jurusan_text.get())
File "D:\py\skripsi\insertData.py", line 23, in insertOrUpdate
self.cursor.execute(query)
File "D:\Program\Python36\lib\site-packages\mysql\connector\cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "D:\Program\Python36\lib\site-packages\mysql\connector\connection_cext.py", line 398, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1