Tkinter Entry变量为空,插入MySQL数据库仅生成空行问题求助
问题分析与解决
你的注册功能插入数据库时生成空数据行,核心原因及修复方案如下:
1. 核心问题:StringVar未绑定到正确窗口实例
每个Tkinter的Tk()对应独立的Tcl解释器,你在Signup函数中创建StringVar()时未指定所属窗口(tk3),导致变量默认绑定到第一个主窗口tk,无法关联tk3中的Entry组件,自然获取不到用户输入值。
2. 额外问题:错误的按钮命令绑定
forgotpassword函数中,按钮的command=PassRecovery()会立刻执行函数,而非点击按钮时触发,需去掉括号。
修正后的完整代码
from tkinter import * import mysql.connector def MainWindow(): global tk tk = Tk() tk.geometry('300x340') tk.title("Login") label = Label(tk, text="Enter Your Credentials", fg="black") label.pack(side="top", pady="15px") label2 = Label(tk, text="Username") label2.pack(expand=True) userlog = StringVar(tk) userentry = Entry(tk, width=20, textvariable=userlog) userentry.pack(expand=True) label3 = Label(tk, text="Password") label3.pack() passlog = StringVar(tk) passentry = Entry(tk, width=20, textvariable=passlog) passentry.pack() login = Button(tk, text="Login", bg="gray") login.pack(pady="15px") forgot = Button(tk, text="Forgot password", bg="white", command=forgotpassword) forgot.pack() btn2 = Button(tk, text="Sign Up", bg="white", command=Signup) btn2.pack(expand=True) tk.mainloop() def Withdraw(): tk2.withdraw() tk.deiconify() def forgotpassword(): global tk2 tk.withdraw() # 用Toplevel替代新Tk(),避免多实例冲突 tk2 = Toplevel(tk) tk2.geometry('300x340') tk2.title("Forgot Password") lbl = Label(tk2, text="Username") lbl.pack(side="top", expand=True) userforgot = StringVar(tk2) usr = Entry(tk2, width=20, textvariable=userforgot) usr.pack(expand=True) lbl2 = Label(tk2, text="Phone Number") lbl2.pack(expand=True) phoneforgot = StringVar(tk2) phone = Entry(tk2, width=20, textvariable=phoneforgot) phone.pack(expand=True) # 去掉括号,点击按钮时才执行函数 btn = Button(tk2, text="Forgot Password", bg="white", command=PassRecovery) btn.pack(expand=True) def PassRecovery(): #connectes to database #checks if datas match or not # if so, creates a label showing the data Btn = Button(tk2, text="Return to login window", command=Withdraw) Btn.pack(expand=True) def Signup(): global tk3, usersign, passsign, phonesign tk.withdraw() # 用Toplevel替代新Tk() tk3 = Toplevel(tk) tk3.geometry('300x340') tk3.title("Sign Up") lbl = Label(tk3, text="Username") lbl.pack(expand=True) # 给StringVar指定master为tk3,绑定到当前窗口 usersign = StringVar(tk3) ent1 = Entry(tk3, textvariable=usersign, width=20) ent1.pack(expand=True) lbl2 = Label(tk3, text="Password") lbl2.pack(expand=True) passsign = StringVar(tk3) ent2 = Entry(tk3, textvariable=passsign, width=20) ent2.pack(expand=True) phonesign = StringVar(tk3) lbl3 = Label(tk3, text="Phone Number") lbl3.pack(expand=True) ent3 = Entry(tk3, textvariable=phonesign, width=20) ent3.pack(expand=True) btn = Button(tk3, text="Sign Up", command=InsertData) btn.pack(expand=True) def InsertData(): db = mysql.connector.connect(host="localhost", user="root", password="Balarooch1386") cursor = db.cursor() cursor.execute("use usercredentials") # 正确获取输入值 username = usersign.get() password = passsign.get() phone = phonesign.get() cursor.execute("insert into data values (%s, %s, %s)", (username, password, phone)) db.commit() labl = Label(tk3, text="Signed Up Successfully") labl.pack(expand=True) # 打印验证获取到的内容 print(f"用户名: {username}, 密码: {password}, 手机号: {phone}") db.close() MainWindow()
关键修改说明
- 所有
StringVar创建时指定对应窗口作为master(如StringVar(tk3)),确保与Entry组件所属窗口绑定 - 用
Toplevel替代多个Tk()实例,避免多解释器导致的变量绑定、事件循环冲突 - 修正按钮命令绑定,去掉不必要的
lambda和括号,确保函数在点击时执行 - 在
InsertData中先获取值再操作,验证输入是否正确
内容的提问来源于stack exchange,提问作者Pigeon
相关产品推荐
相关产品推荐

