简体   繁体   English

从条目中检索数据

[英]Retrieving data from entry

When I hit the button in this script I get an error saying 当我点击此脚本中的按钮时,我收到错误消息

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'

What am I doing wrong here? 我在这做错了什么?

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

Look at this line: 看看这一行:

e1 = Entry(root, width=10).grid(row=r, column=1)

In this case this does the same as: 在这种情况下,这与:

e1 = Entry(root, width=10)
e1 = e1.grid(row=r, column=1)

e1.grid(...) returns None, so you end up with e1 = None . e1.grid(...)返回None,因此最终得到e1 = None As you use this kind of assignment everywhere, your lists are filled with None only, therefore when you later call ... e1.get() ... on them, you get that error. 当你在任何地方使用这种赋值时,你的列表只用None填充,因此当你稍后在它们上面调用... e1.get() ...时,你会得到那个错误。

Try to separate creating the objects and calling methods on them. 尝试分离创建对象和调用它们的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM