如何删除添加退出按钮时发生的错误?

How to remove error made when adding exit button?

from tkinter import*
import sqlite3
class login:
    def __init__(self,root):
        self.root=root
        self.root.geometry("250x250")
        self.root.title("Login")
        self.root.resizable(False,False)

        self.var_username=StringVar()   ##variables
        self.var_password=StringVar()

        username=Label(self.root,text="Username",font=("Bahnschrift SemiBold",15),bg="white",fg="black").place(x=15,y=20) 
        username=Entry(self.root,textvariable=self.var_username,font=("Bahnschrift SemiBold",15),bg="white",fg="black").place(x=125,y=20,width=115)

        password=Label(self.root,text="Password ",font=("Bahnschrift SemiBold",15),bg="white",fg="black").place(x=15,y=60)
        password=Entry(self.root,textvariable=self.var_password,font=("Bahnschrift SemiBold",15),bg="white",fg="black").place(x=125,y=60,width=115)
    
        _exit=Button(self.root,text="exit",command=self.destroy,font=("Bahnschrift SemiBold",15),bg="green",fg="white",cursor="hand2").place(x=125,y=100,width=55,height=28)

if __name__ == "__main__":
root=Tk()
obj=login(root)
root.mainloop()

它出现了一个属性错误,我不确定如何修复它,因为它适用于其他代码段。

您可能希望将按钮命令从 self.destroy 更改为 self.root.destroy

Button(self.root,
    text="exit",
    command=self.root.destroy,
    font=("Bahnschrift SemiBold",15),
    bg="green",
    fg="white",
    cursor="hand2").place(x=125, y=100, width=55, height=28)

作为旁注,执行 label = Label(root, ...).place(x=...) 不会执行任何操作(对于任何小部件)。 label 的值将存储为 None,您以后将无法引用它来更改其属性。如果这是目标,那么只需: Label(root, ...).place(x=...) 就可以了。否则,您将不得不在一行中创建小部件,并将它们放在下一行

PS: 建议的做法是还包括所面临的错误以便于诊断。请参考 How to ask 提出更多问题。