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

使用Tkinter的.get()方法时触发AttributeError错误求助

问题排查与修复

错误原因分析

  • 组件对象赋值错误:Tkinter中Entry()创建组件后,直接链式调用.place()(或.pack()/.grid())时,返回值是None而非组件实例。你把这个None赋值给了self.username_entry等变量,导致后续调用.get()时触发NoneType属性错误。
  • 方法调用错误:代码中self.username_entry.get!=self.confirm_username_entry.get一行,get是方法,必须加括号get()才能调用获取输入内容,否则是在比较方法对象本身,逻辑完全错误。
  • 拼写错误:提示框中的"Passwrods"是拼写错误,应为"Passwords"。

修复后的完整代码

from tkinter import *
from tkinter import messagebox

window = Tk()
window.geometry("1650x1000")
window.title("Sign up")


class sign_up_screen():
    def __init__(self, master):
        self.master = master
        self.master.geometry("1650x1000")
        self.master.title("Sign up")
        self.setup_window()

    def setup_window(self):
        signup_label = Label(self.master, text="sign up page", font=("Arial Bold", 50,), fg='#0066ff')
        signup_label.pack()

        username_label = Label(self.master, text="username", font=("Arial Bold", 25,))
        username_label.place(x=600, y=200)
        self.username_entry = Entry(width=10, font=("Arial", 25))
        self.username_entry.place(x=770, y=200)

        password_label = Label(self.master, text="password", font=("Arial Bold", 25,))
        password_label.place(x=600, y=250)
        self.password_entry = Entry(width=10, font=("Arial", 25))
        self.password_entry.place(x=770, y=250)

        confirm_username_label = Label(self.master, text="confirm username", font=("Arial Bold", 25,))
        confirm_username_label.place(x=467, y=300)
        self.confirm_username_entry = Entry(width=10, font=("Arial", 25))
        self.confirm_username_entry.place(x=770, y=300)

        confirm_password_label = Label(self.master, text="confirm password", font=("Arial Bold", 25,))
        confirm_password_label.place(x=467, y=350)
        self.confirm_password_entry = Entry(width=10, font=("Arial", 25))
        self.confirm_password_entry.place(x=770, y=350)

        register_button = Button(self.master, text="sign up", font=("Arial Bold", 25,),
                               bg="black", fg="white", command=self.signup)
        register_button.place(x=750, y=500)

    def signup(self):
        # 注册验证逻辑
        username = self.username_entry.get()
        confirm_username = self.confirm_username_entry.get()
        password = self.password_entry.get()
        confirm_password = self.confirm_password_entry.get()

        if username != confirm_username:
            messagebox.showerror("Error", "Usernames do not match")
            return

        if len(username) < 6:
            messagebox.showerror("Error", "Minimum of 6 characters for username")
            return

        if password != confirm_password:
            messagebox.showerror("Error", "Passwords do not match")
            return

        if len(password) < 6:
            messagebox.showerror("Error", "Minimum of 6 characters for password.")
            return


s = sign_up_screen(window)
window.mainloop()

关键修复点

  • 将组件创建和布局方法(place())拆分到两行,确保self.username_entry等变量存储的是Entry组件实例,而非None。
  • 所有调用get()时添加括号,正确获取输入框内容。
  • 修正拼写错误,统一提示文本的准确性。
  • 提前将所有输入框内容存入变量,让代码更简洁易读。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:55:22