1

嗨,我正在尝试创建一个简单的触摸屏界面,允许用户在输入小部件中输入 4 位代码,然后将其保存为字符串。我不确定如何执行以下操作:当按下按钮将该值输入到 Entry 小部件时,这是我的代码,但我收到以下错误:

AttributeError:“NoneType”对象没有属性“插入”

   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()
4

1 回答 1

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)

笔记:

  • 您对 L1 变量有同样的问题
  • 您可能还想向其他按钮添加命令

通过从评论中解决新问题,您的代码将变为:

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)          
于 2014-02-26T13:30:07.567 回答