为什么我得到AttributeError:Python和Tkinkter的“\'NoneType\'对象没有属性\'get\'”?

问题描述

|
from tkinter import *

app=Tk()

app.title(\" BRAIN SYNCRONIZATION SOFTWARE \")

e1=Entry(app).pack()
t1=Text(app).pack()

def InputFun():
        file=open(\"acad.txt\",\"a\")
        file.write(\"%s;%s\"%(t1.get(\"0.1\",END),e1.get()))
        file.close()
b1=Button(app,text=\"INPUT\",command=InputFun,height=3,width=4).pack(side=LEFT,padx=30,pady=30)
这是我编写的代码,但是当我按下输入按钮时,我反复得到以下错误
Exception in Tkinter callback
Traceback (most recent call last):
  File \"C:\\python31\\lib\\tkinter\\__init__.py\",line 1399,in __call__
    return self.func(*args)
  File \"C:\\Users\\vonn\\Desktop\\brain syncronization.py\",line 15,in InputFun
    file.write(\"%s\"%t1.get(\"0.1\",END))
AttributeError: \'nonetype\' object has no attribute \'get\'
为什么不写文件?     

解决方法

        
t1=Text(app).pack()
应该
t1=Text(app)
t1.pack()
Tkinkter的
pack()
方法返回None,您不能在其上运行
.get()
,但是需要保持
t1
指向文本对象本身。     ,        我认为Entry(app).pack()不会返回任何内容。你是说ѭ7吗?