[英]Python/MySQL - Insert data into table by Tkinter
我試圖通過圖形界面向表中插入數據,但出現以下錯誤:
錯誤
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\TESTES\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
TypeError: submit() missing 3 required positional arguments: 'nome', 'idade', and 'cpf'
代碼
from tkinter import *
import pymysql
book = Tk()
book.geometry("400x300")
book.title("test")
##-----------------##
## CONEXÃO BANCO ##
##-----------------##
conn = pymysql.connect(host="localhost", port=3306, user="root", password="", database="oneday")
print("connect successfull! goodwork bro screw screww")
cursor = conn.cursor()
cursor.execute("SELECT VERSION()")
versao = cursor.fetchone()
print("Versão do gerenciador Maria DB: %s" % versao)
conn.close()
##-----------------##
## STRINGVAR ##
##-----------------##
##-----------------##
## FUNÇÕES/DEF'S ##
##-----------------##
def submit(nome, idade, cpf):
nome = StringVar()
idade = StringVar()
cpf = StringVar()
try:
conn = pymysql.connect(host="localhost", port=3306, user="root", password="", db="oneday")
cursor = conn.cursor()
mysql_submit = ("INSERT INTO novo VALUES (NULL, {}, {}, {})", (nome.get(), idade.get(), cpf.get()))
cursor.execute(mysql_submit)
conn.commit()
print("Dados inseridos com sucesso na tabela.")
cursor.close()
conn.close()
except pymysql.connector.Error as error:
print("failed".format(error))
finally:
if (conn.connect()):
cursor.close()
conn.close()
print("MySQL connection is closed")
# Clear the text boxes
e_nome.delete(0, END)
e_idade.delete(0, END)
e_cpf.delete(0, END)
def closer():
try:
conn = pymysql.connect(host="localhost", port=3306, user="root", password="", db="oneday")
cursor = conn.cursor()
mysql_closer = """DELETE FROM novo"""
cursor.execute(mysql_closer)
conn.commit()
print("Dados removidos com sucesso da tabela.")
cursor.close()
conn.close()
except pymysql.connector.Error as error:
print("failed".format(error))
finally:
if (conn.connect()):
cursor.close()
conn.close()
print("MySQL connection is closed")
##-----------------##
## LABELS ##
##-----------------##
mylabel = LabelFrame(book, text="Novo")
mylabel.place(x=10, y=10, width=300, height=150)
l_nome = Label(book, text="Nome")
l_nome.place(x=15, y=30)
e_nome = Entry(book)
e_nome.place(x=100, y=30)
l_idade = Label(book, text="Idade")
l_idade.place(x=15, y=60)
e_idade = Entry(book)
e_idade.place(x=100, y=60)
l_cpf = Label(book, text="CPF")
l_cpf.place(x=15, y=90)
e_cpf = Entry(book)
e_cpf.place(x=100, y=90)
##-----------------##
## BOTOÕES ##
##-----------------##
bt = Button(book, width=15, text="Gravar", command=submit)
bt.place(x=100, y=175)
bt = Button(book, width=15, text="Sair", command=exit)
bt.place(x=100, y=245)
bt = Button(book, width=15, text="Apagar", command=closer)
bt.place(x=100, y=210)
##-----------------##
## IMAGEM ##
##-----------------##
photo = PhotoImage(file="C:/Users/TESTES/PycharmProjects/OMNIA/images/logo.png")
logo = Label(book, image=photo)
logo.place(x=250, y=175, width=100, height=100)
logo.background = "orange"
book.mainloop()
Button
運行沒有參數的函數,所以你不能用參數聲明它
def submit():
它應該可以解決錯誤消息的問題,但您仍然會遇到獲取值的問題。
您的StringVar()
在submit()
中沒用。 您應該直接在 SQL 中使用 Entries
mysql_submit = ("INSERT INTO novo VALUES (NULL, {}, {}, {})", (e_nome.get(), e_idade.get(), e_cpf.get()))
但是您應該將它直接放在execute
因為它期望QUERY
作為第一個參數,將值作為第二個參數。 您的元組mysql_submit
將被視為一個參數,而不是兩個。
cursor.execute("INSERT INTO novo VALUES (NULL, {}, {}, {})", (e_nome.get(), e_idade.get(), e_cpf.get()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.