我不知道为什么它不起作用,即使当我复制一些代码时它也起作用,但当我编写代码时它却不起作用。
我想得到用户名和密码,这样我就可以验证登录名,否则它会显示一个消息框。
from tkinter import *
def login():
usrnam = username.get()
paswrd = password.get()
print(username)
root = Tk()
photo = PhotoImage(file='small logo.png')
root.geometry('300x500+450+100')
root.title('Clinique nouadhibou')
root.config(background='white')
root.iconphoto(True,photo)
label1 = Label(root, text="bonjour !", font=('Arial', 20, 'bold'), bg='white', fg='green', relief=RAISED, bd=10, image=photo, compound='top').pack()
username = Entry(root).place(x=95, y=320)
password = Entry(root, show='*').place(x=95, y=340)
who_is_this = Checkbutton(root).place(x=0, y=480)
button = Button(root, text='Enter !', command=login).place(x=137, y=360)
button = Button(root, text='SORTIR!', command=quit).place(x=130, y=470)
root.mainloop()
它给了我这个错误:
File "clinique.py", line 14, in login
usrnam = username.get()
AttributeError: 'NoneType' object has no attribute 'get'
.place()
返回None
。您应该在一行中声明小部件,并将其放在下一行中,然后引用声明。
示例:
# old code
username = Entry(root).place(x=95, y=320)
password = Entry(root, show='*').place(x=95, y=340)
# new code
username = Entry(root)
username.place(x=95, y=320)
password = Entry(root, show='*')
password.place(x=95, y=340)