You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter中'StringVar'对象不可调用及类型错误问题求助

Tkinter+SQLite注册界面错误修复方案

错误1:'StringVar' object is not callable

  • 问题根源:submit方法的SQL插入语句中,错误将self.Fullname(StringVar对象)当作函数调用(self.Fullname()),StringVar必须通过.get()方法获取值。
  • 附带问题:SQL表结构与插入参数不匹配——原表仅定义3个字段,但代码传入了4个值(用户名、密码、确认密码、全名),需修改表结构添加Fullname字段。

错误2:异常处理参数类型不匹配

  • 问题根源:messagebox.showerror的第二个参数要求是字符串或None,但代码直接传入了Exception对象,需将异常转换为字符串形式str(ep)。

修正后的完整代码

import sqlite3
from tkinter import *
from tkinter import messagebox

class Register:
    def __init__(self, window):
        self.Username = StringVar()
        self.Password = StringVar()
        self.Confirm_Password = StringVar()
        self.Fullname = StringVar()
        self.window = window
        self.window.geometry('500x250')
        window.resizable(0, 0)
        window.configure(bg="deep sky blue")
        window.title("Register")

        # 全名标签与输入框
        l_fullname = Label(self.window, text="Full Name:", font=("Arial", 15), bg="deep sky blue")
        l_fullname.place(x=10, y=10)
        self.fullname_entry = Entry(self.window, width=30, bd=5, textvariable=self.Fullname)
        self.fullname_entry.place(x=200, y=10)

        # 用户名标签与输入框
        l_username = Label(self.window, text="Username:", font=("Arial", 15), bg="deep sky blue")
        l_username.place(x=10, y=60)
        self.username_entry = Entry(self.window, width=30, bd=5, textvariable=self.Username)
        self.username_entry.place(x=200, y=60)

        # 密码标签与输入框
        l_password = Label(self.window, text="Password:", font=("Arial", 15), bg="deep sky blue")
        l_password.place(x=10, y=110)
        self.password_entry = Entry(self.window, width=30, show="*", bd=5, textvariable=self.Password)
        self.password_entry.place(x=200, y=110)

        # 确认密码标签与输入框
        l_confirm_pwd = Label(self.window, text="Confirm Password:", font=("Arial", 15), bg="deep sky blue")
        l_confirm_pwd.place(x=10, y=160)
        self.confirm_pwd_entry = Entry(self.window, width=30, show="*", bd=5, textvariable=self.Confirm_Password)
        self.confirm_pwd_entry.place(x=200, y=160)

        # 按钮
        b_submit = Button(self.window, text="Sign Up", font=("Arial", 15), bg="#ffc22a", command=self.submit)
        b_submit.place(x=150, y=210)
        b_back = Button(self.window, text="Back", font=("Arial", 15), bg="#ffc22a", command=self.window.destroy)
        b_back.place(x=250, y=210)

        # 初始化数据库表
        self.init_db()

    def init_db(self):
        connection = sqlite3.connect('RegLog.db')
        cur = connection.cursor()
        # 添加Fullname字段,匹配输入参数
        cur.execute("CREATE TABLE IF NOT EXISTS RegLog(Fullname TEXT, Username TEXT, Password TEXT, ConfirmPassword TEXT)")
        connection.commit()
        connection.close()

    def submit(self):
        check_counter = 0
        warn = ""
        # 校验全名
        if self.fullname_entry.get() == "":
            warn = "Full Name can't be empty"
        else:
            check_counter += 1
        # 校验用户名
        if self.username_entry.get() == "":
            warn = "Username can't be empty"
        else:
            check_counter += 1
        # 校验密码
        if self.password_entry.get() == "":
            warn = "Password can't be empty"
        else:
            check_counter += 1
        # 校验确认密码
        if self.confirm_pwd_entry.get() == "":
            warn = "Confirm Password can't be empty"
        else:
            check_counter += 1
        # 校验密码一致性
        if self.password_entry.get() != self.confirm_pwd_entry.get():
            warn = "Passwords didn't match!"
        
        if check_counter == 4 and not warn:
            try:
                connection = sqlite3.connect("RegLog.db")
                cur = connection.cursor()
                # 修正StringVar调用方式,匹配表字段顺序
                cur.execute("INSERT INTO RegLog VALUES(?,?,?,?)",
                            (self.Fullname.get(), self.Username.get(), self.Password.get(), self.Confirm_Password.get()))
                connection.commit()
                connection.close()
                messagebox.showinfo("Success", "New account created successfully")
                # 清空输入框
                self.Fullname.set("")
                self.Username.set("")
                self.Password.set("")
                self.Confirm_Password.set("")
            except Exception as ep:
                # 将异常转换为字符串传入弹窗
                messagebox.showerror('Error', str(ep))
        else:
            messagebox.showerror('Error', warn)

def main():
    window = Tk()
    Register(window)
    window.mainloop()

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者cj_123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 17:20:58