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

时间:2011-06-12 03:27:54

标签: python

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'

为什么不写文件?

2 个答案:

答案 0 :(得分:9)

t1=Text(app).pack()

应该是

t1=Text(app)
t1.pack()

Tkinkter pack()方法返回None,您无法在其上运行.get(),但需要保持t1引用文本对象本身。

答案 1 :(得分:0)

我不认为Entry(app).pack()会返回任何内容。你的意思是e1=Entry(app); e1.pack()