如何确保从Tkinter表单的值被输入到MySQL表,而不是得到一个错误消息?



我做了一个tkinter表单,它应该输入值到MySQL表中。然而,当我在表单中输入适当的值时,我得到了由我创建的错误消息("所有字段都是必需的")。在下面的代码中,a=task_name.get() [string], b=student_subject.get() [string], c=due_date.get() [int], d=priority.get() [int], e=task_type.get() [string], f= date_deleted .get() [int]。

def action():
if a=="" or b=="" or e=="":
messagebox.showerror("Error" , "All fields are required" , parent = wintask)
else:
con = mysql.connect(host="localhost", user="root", password="12345678", database="ibcomputerscience")
cur = con.cursor()
cur.execute("INSERT INTO task_info(student_subject, task_name, due_date, priority, task_type, date_added) VALUES(%s,%s,%s,%s,%s,%s)",(a, b, c, d, e, f))
con.commit()
con.close()
messagebox.showerror("Success" , "Task Added Successfully", parent = wintask)
我将感激任何帮助/见解。谢谢你!

您需要在action()中调用这些.get():

def action():
a = student_subject.get()
b = task_name.get()
c = due_date.get()
d = priority.get()
e = task_type.get()
f = date_added.get()
if a == "" or b == "" or e == "":
messagebox.showerror("Error", "All fields are required", parent=wintask)
else:
con = mysql.connect(host="localhost", user="root", password="12345678", database="ibcomputerscience")
cur = con.cursor()
cur.execute("INSERT INTO task_info(student_subject, task_name, due_date, priority, task_type, date_added) VALUES(%s,%s,%s,%s,%s,%s)",(a, b, c, d, e, f))
con.commit()
con.close()
messagebox.showinfo("Success", "Task Added Successfully", parent=wintask)

最新更新