Tkinter,从条目

时间:2016-05-11 19:46:52

标签: python user-interface tkinter tkinter-entry

我正在使用Tkinter在python中创建一个简单的GUI程序。问题是,当我尝试使用按钮从我的条目中检索值时,它会返回error()

print(ex1.get())
AttributeError: 'NoneType' object has no attribute 'get'

如何避免这种情况?

这是我的代码:

root = Tk()

root.minsize(300,300)
root.maxsize(300,300)


n = ttk.Notebook()

f1 = Frame(n,height=280,width=280)
f1.propagate(0)
lx1 = Label(f1,text="x",width=5).grid(row = 1,column=0) 
ex1 = Entry(f1,width = 10).grid(row = 1,column = 1)

ly1 = Label(f1,text="y",width=5).grid(row=3,column=0)
ey1 = Entry(f1,width = 10).grid(row=3,column = 1)

def value():
    print(ex1.get())


Bcreate = Button(f1,text="CREATE",command=value).grid(row = 10,column =  5)
n.add(f1,text="add point")

f2 = Frame(n)
n.add(f2,text="draw line")


n.pack()

1 个答案:

答案 0 :(得分:1)

请阅读如何发布正确的MCVE

以下是您的问题:

from tkinter import Tk, Entry

root = Tk()
ent = Entry(root).pack()
print(ent)
print(ent.get())

打印

None
Traceback (most recent call last):
  File "F:\Python\mypy\tem.py", line 6, in <module>
    print(ent.get())
AttributeError: 'NoneType' object has no attribute 'get'

此时,.pack()显然会返回None。所以改为绑定Entry返回的小部件。

from tkinter import Tk, Entry

root = Tk()
ent = Entry(root)
ent.pack()
print(ent)
print(ent.get())

获取

.2425099144832
got  .