-1
我目前正在編寫一個GUI,允許您在列表框中顯示字典條目,我希望能夠突出顯示某些內容,並在按鈕上按下執行某些功能,並在這些條目中包含信息。通過列表框訪問字典
但我目前得到一個KeyError '0'
請參閱代碼(bewarned這是一個有點冗長):
#!/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()
如果任何人都可以看看,看看它是否只是我想要的方式訪問字典,或者如果有某個地方會有大的錯誤,將不勝感激。
乾杯