我目前正在编写一个允许您在列表框中显示字典条目的GUI,我希望能够突出显示某些字典条目,并且按下按钮执行某些功能,并在这些条目中包含信息。
但我目前正在获得KeyError '0'
请参阅代码(警告它有点冗长):
#!/usr/bin/python
import Tkinter as tk
from os import system
transDict = {}
transCount = 0
class TransMsg(object):
def __init__(self):
self.canID = ""
self.msgType = ""
self.canType = ""
self.tData0 = ""
self.tData1 = ""
self.tData2 = ""
self.tData3 = ""
self.tData4 = ""
self.tData5 = ""
self.tData6 = ""
self.tData7 = ""
self.timer = 0
self.DLC = 0
class application:
def __init__(self,window):
self.window = window
"""Initialize the Application """
self.IDbox = tk.Entry(window, width = 3)
self.IDbox.pack(side="left", expand=True)
self.IDbox.insert(0,"ID")
self.IDbox.grid(row = 0, column = 0)
self.msgTypeBox = tk.Entry(window,width = 8)
self.msgTypeBox.pack(side="left", expand=True)
self.msgTypeBox.insert(0,"MSG Type")
self.msgTypeBox.grid(row = 0, column = 1)
self.canTypeBox = tk.Entry(window,width = 8)
self.canTypeBox.pack(side="left", expand=True)
self.canTypeBox.insert(0,"Can Type")
self.canTypeBox.grid(row = 0, column = 2)
self.tData0Box = tk.Entry(window, width = 3)
self.tData0Box.pack(side="left", expand=True)
self.tData0Box.insert(0,"FF")
self.tData0Box.grid(row = 0, column = 3)
self.tData1Box = tk.Entry(window, width = 3)
self.tData1Box.pack(side="left", expand=True)
self.tData1Box.insert(0,"FF")
self.tData1Box.grid(row = 0, column = 4)
self.tData2Box = tk.Entry(window, width = 3)
self.tData2Box.pack(side="left", expand=True)
self.tData2Box.insert(0,"FF")
self.tData2Box.grid(row = 0, column = 5)
self.tData3Box = tk.Entry(window, width = 3)
self.tData3Box.pack(side="left", expand=True)
self.tData3Box.insert(0,"FF")
self.tData3Box.grid(row = 0, column = 7)
self.tData4Box = tk.Entry(window, width = 3)
self.tData4Box.pack(side="left", expand=True)
self.tData4Box.insert(0,"FF")
self.tData4Box.grid(row = 0, column = 8)
self.tData5Box = tk.Entry(window, width = 3)
self.tData5Box.pack(side="left", expand=True)
self.tData5Box.insert(0,"FF")
self.tData5Box.grid(row = 0, column = 9)
self.tData6Box = tk.Entry(window, width = 3)
self.tData6Box.pack(side="left", expand=True)
self.tData6Box.insert(0,"FF")
self.tData6Box.grid(row = 0, column = 10)
self.tData7Box = tk.Entry(window, width = 3)
self.tData7Box.pack(side="left", expand=True)
self.tData7Box.insert(0,"FF")
self.tData7Box.grid(row = 0, column = 11)
self.tTimerBox = tk.Entry(window, width = 3)
self.tTimerBox.pack(side="left", expand=True)
self.tTimerBox.insert(0,"0")
self.tTimerBox.grid(row = 1, column = 0)
self.tTimerBox.bind("<Return>",self.addToList)
self.TranButton = tk.Button(window,
text="Transmit",
command = self.Enter)
self.TranButton.pack(side="bottom")
self.TranButton.grid(row = 1, column = 11)
self.Translist = tk.Listbox(window,selectmode = "multiple", height =10, width = 50)
self.Translist.pack(side = "bottom", expand = True)
self.Translist.grid(row = 2, columnspan=11)
def addToList(self,event):
global transDict
dictRef = len(transDict) + 1
t = TransMsg()
t.canID = self.IDbox.get()
t.msgType = self.msgTypeBox.get()
t.canType = self.canTypeBox.get()
t.DLC = 8
t.tData0 = self.tData0Box.get()
t.tData1 = self.tData1Box.get()
t.tData2 = self.tData2Box.get()
t.tData3 = self.tData3Box.get()
t.tData4 = self.tData4Box.get()
t.tData5 = self.tData5Box.get()
t.tData6 = self.tData6Box.get()
t.tData7 = self.tData7Box.get()
t.tTimer = self.tTimerBox.get()
transDict[dictRef] = t
self.Translist.insert("end","%d: %s %d %s %s %s %s %s %s %s %s %d" %
(dictRef,
transDict[dictRef].canID,
transDict[dictRef].DLC,
transDict[dictRef].tData0,
transDict[dictRef].tData1,
transDict[dictRef].tData2,
transDict[dictRef].tData3,
transDict[dictRef].tData4,
transDict[dictRef].tData5,
transDict[dictRef].tData6,
transDict[dictRef].tData7,
int(transDict[dictRef].tTimer)))
def Enter(self):
items = self.Translist.curselection()
for i in items:
print transDict[i].tTimer
if transDict[i].tTimer <= 0:
system('echo "sending: %s %s 0x%s %d 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s"' %
(transDict[i].msgType,
transDict[i].canType,
transDict[i].canID,
transDict[i].DLC,
transDict[i].tData0,
transDict[i].tData1,
transDict[i].tData2,
transDict[i].tData3,
transDict[i].tData4,
transDict[i].tData5,
transDict[i].tData6,
transDict[i].tData7))
else:
system('echo "sending: %s %s 0x%s %d 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s 0x%s"' %
(transDict[i].msgType,
transDict[i].canType,
transDict[i].canID,
transDict[i].DLC,
transDict[i].tData0,
transDict[i].tData1,
transDict[i].tData2,
transDict[i].tData3,
transDict[i].tData4,
transDict[i].tData5,
transDict[i].tData6,
transDict[i].tData7))
self.window.after(transDict[i].tTimer, self.Enter)
root = tk.Tk()
myapp = application(root)
root.mainloop()
如果有人可以看一下,看看它是否只是我试图访问字典的方式,或者某个地方是否存在重大错误,将会非常感激。
答案 0 :(得分:1)
这是因为您正在执行dictRef = len(transDict) + 1
,这使dictRef
从1开始。然而,列表框索引从0开始。因此,当选择第一个列表框项时, i
将为0,但字典中的键0没有任何内容
因此,要么使用dictRef = len(transDict)
,要么dictRef
与列表框索引(我建议)相同,要么i += 1
之后for i in items:
来补偿差异。< / p>
另一方面,请不要在同一个小部件上同时使用pack
和grid
,这绝对没有意义。 pack
和grid
是两个不同的几何管理器,因此您可以在窗口中一次只放置一个窗口小部件。