首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从条目中检索数据

从条目中检索数据
EN

Stack Overflow用户
提问于 2013-04-15 03:40:15
回答 1查看 120关注 0票数 0

当我点击这个脚本中的按钮时,我得到一个错误消息:

代码语言:javascript
复制
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
 File "grid_layout.py", line 41, in printout
 data = [ { l1.cget("text"): (e1.get(), e2.get()) } for e1,e2,l1 in zip(E1,E2,L1) ]
AttributeError: 'NoneType' object has no attribute 'get'

我在这里做错了什么?

代码语言:javascript
复制
from Tkinter import *
import json


root = Tk(  )

E1 = []
E2 = []
E3 = []
L1 = []

Label(root, text="Way Point").grid(row=0, column=0)
Label(root, text="x").grid(row=0, column=1)
Label(root, text="y").grid(row=0, column=2)
Label(root, text="z").grid(row=0, column=3)

for r in range(1,10):
    l1 = Label(root, text="Number%d" % (r),borderwidth=1).grid(row=r, column=0)
    e1 = Entry(root, width=10).grid(row=r, column=1)
    e2 = Entry(root, width=10).grid(row=r, column=2)
    e3 = Entry(root, width=10).grid(row=r, column=3)
    E1.append(e1)
    E2.append(e2)
    E3.append(e3)
    L1.append(l1)

Label(root, text="zone").grid(row=11, column=0)
Label(root, text="x").grid(row=11, column=1)
Label(root, text="y").grid(row=11, column=2)
Label(root, text="z").grid(row=11, column=3)

for r in range(12,22):
    l2 = Label(root, text="Number%d" % (r-11),borderwidth=1 ).grid(row=r, column=0)
    e4 = Entry(root, width=10).grid(row=r, column=1)
    e5 = Entry(root, width=10).grid(row=r, column=2)
    e6 = Entry(root, width=10).grid(row=r, column=3)

def printout():
 # Iterate over the zip of E & L (joined), building the dict using .cget('text') to get
 # the value of the Tkinter label. Add the { 'c':3.0 } to the end of the resulting list
    data = [ { l1.cget("text"): (e1.get(), e2.get()) } for e1,e2,l1 in zip(E1,E2,L1) ]
    print json.dumps(data, sort_keys=True, indent=2)
    with open('data.json', 'w') as outfile:
        json.dump(data, outfile, sort_keys=True, indent=2)

plus = Button(root,  text='Print', command=printout)
plus.grid(row=23, column=1)

root.mainloop(  )
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-15 03:46:59

看这一行:

代码语言:javascript
复制
e1 = Entry(root, width=10).grid(row=r, column=1)

在本例中,这与以下操作相同:

代码语言:javascript
复制
e1 = Entry(root, width=10)
e1 = e1.grid(row=r, column=1)

e1.grid(...)返回None,因此您将以e1 = None结束。由于您在任何地方都使用这种赋值,您的列表中只填充了None,因此当您稍后对它们调用... e1.get() ...时,您会得到该错误。

尝试将创建对象和调用对象的方法分开。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16003475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档