如何消除添加退出按钮时出现的错误?

问题描述 投票:0回答:1
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()

它出现属性错误,我不确定如何修复它,因为它可以在其他代码段中工作。

python database sqlite tkinter button
1个回答
2
投票

您可能想要将按钮命令从

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=...)
就可以了。否则,您必须在一行中创建小部件,并将它们放在下一行中。

© www.soinside.com 2019 - 2024. All rights reserved.