您好我正在尝试创建一个简单的触摸屏界面,允许用户在条目小部件中输入4位数代码,然后将其保存为字符串。我不确定如何执行以下操作: 按下按钮时将该值输入Entry小部件 这是我的代码到目前为止,但我得到以下错误:
AttributeError:'NoneType'对象没有属性'insert'
def lockscreen():
locks = Toplevel(width=500,height=500)
locks.title('Lock Screen')
L1 = Label(locks,text="Enter 4 Digit Lock Code").grid(row=1,column=1,columnspan=3)
e1=Entry(locks, bd=5).grid(row=2,column=1,columnspan=3)
Button(locks, width=3, height=3, text='1', command =lambda:screen_text("1")).grid(row=3,column=1)
Button(locks, width=3, height=3, text='2').grid(row=3,column=2)
Button(locks, width=3, height=3, text='3').grid(row=3,column=3)
Button(locks, width=3, height=3, text='4').grid(row=4,column=1)
Button(locks, width=3, height=3, text='5').grid(row=4,column=2)
Button(locks, width=3, height=3, text='6').grid(row=4,column=3)
Button(locks, width=3, height=3, text='7').grid(row=5,column=1)
Button(locks, width=3, height=3, text='8').grid(row=5,column=2)
Button(locks, width=3, height=3, text='9').grid(row=5,column=3)
Button(locks, width=3, height=3, text='Close').grid(row=6,column=1)
Button(locks, width=3, height=3, text='0').grid(row=6,column=2)
Button(locks, width=3, height=3, text='Enter').grid(row=6,column=3)
def screen_text(text):
e1.insert(0,text)
return
master.mainloop()
答案 0 :(得分:1)
问题在于这一行:
e1=Entry(locks, bd=5).grid(row=2,column=1,columnspan=3)
通过将Entry()构造函数和grid()调用链接在一起,您实际上将grid()
调用的结果存储在e1
中,而不是Entry字段中。修复:
e1=Entry(locks, bd=5)
e1.grid(row=2,column=1,columnspan=3)
注意:
通过评论解决新问题,您的代码就像:
def lockscreen():
locks = Toplevel(width=500,height=500)
locks.title('Lock Screen')
L1 = Label(locks,text="Enter 4 Digit Lock Code")
L1.grid(row=1,column=1,columnspan=3)
e1=Entry(locks, bd=5)
e1.grid(row=2,column=1,columnspan=3)
def screen_text(text):
e1.insert(0,text)
Button(locks, width=3, height=3, text='1',
command=lambda:screen_text("1")).grid(row=3,column=1)