我有一个更新列表框条目的功能,通过删除并使用更新的计数器重新插入它们,但是当我尝试维护突出显示的项目时会出现困难,因为它已被删除,没有什么可以突出显示。
我的问题是如何让程序记住突出显示的内容?
代码:
#!/usr/bin/python
from time import clock, sleep
from os import system
from collections import defaultdict
from subprocess import PIPE, Popen
from threading import Thread, Lock
import Tkinter as tk
import Queue
import signal
import os, sys
# -------------------------------------------------------------------
# Global Definitions
mydict ={} # define the dictionary
dict_lock = Lock()
transList = []
running = 0
Counter = 0
edit_ref= 0
RecvQueue = Queue.Queue()
terminate = 0
pcanbus = "/dev/pcanusb0"
# ***************************************************************
#
# ************************ PCAM Msg ************************
#
# ***************************************************************
class PCANmsg(object):
def __init__(self):
self.dlc = 0
self.CANtime = 0
self.PCANperiod = 0
self.COUNT = 0
self.hdata0 = 0
self.hdata1 = 0
self.hdata2 = 0
self.hdata3 = 0
self.hdata4 = 0
self.hdata5 = 0
self.hdata6 = 0
self.hdata7 = 0
self.timing = 0
class ThreadedTask(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
global proc
payload = iter(proc.stdout.readline, "")
for line in payload:
if line[0].isdigit():
splitline = line.split()
self.dictAdd(splitline)
def dictAdd(self, info):
global mydict
try:
can_ID = info[4]
p = PCANmsg()
p.dlc = int(info[5])
p.CANtime = float(info[0])
p.hdata0 = info[6]
p.hdata1 = info[7]
p.hdata2 = info[8]
p.hdata3 = info[9]
p.hdata4 = info[10]
p.hdata5 = info[11]
p.hdata6 = info[12]
p.hdata7 = info[13]
p.timing = 1
if can_ID in mydict.keys():
q = mydict[can_ID]
p.COUNT = q.COUNT + 1
p.PCANperiod = p.CANtime - q.CANtime
else:
p.COUNT = 1
p.PCANperiod = 0.0
mydict[can_ID] = p
s = '%06X : %3d %02X %02X %02X %02X %02X %02X %02X %02X\t %8.2F %8d ' %\
(int(can_ID,16),
p.dlc,
int(p.hdata0, 16),
int(p.hdata1, 16),
int(p.hdata2, 16),
int(p.hdata3, 16),
int(p.hdata4, 16),
int(p.hdata5, 16),
int(p.hdata6, 16),
int(p.hdata7, 16),
p.PCANperiod,
p.COUNT)
self.queue.put(s)
except IndexError:
pass
# ***************************************************************
#
# ************************ RecvTran GUI *************************
#
# ***************************************************************
class application:
def __init__(self,window):
self.window = window
self.window.title("PYCAN")
"""Initialize the Application """
window.grid_rowconfigure(9,weight=1)
self.RecvButton = tk.Button(window,
text = "Receive",
command = self.RecvBtn_Click)
self.RecvButton.grid(row = 9, column = 12)
self.TransLabel = tk.Label(window, text = "Messages Received:")
self.TransLabel.grid(row = 8, columnspan = 2)
self.RecvList = tk.Listbox(window, height = 10, width = 60, exportselection=False)
self.RecScrol = tk.Scrollbar(window)
self.RecScrol.grid(row=9, column=11, sticky=tk.NW+tk.SE)
self.RecvList.grid(row = 9, rowspan = 3, columnspan = 11, sticky=tk.NW+tk.SE)
self.RecScrol.config(command=self.RecvList.yview)
self.RecvList.config(yscrollcommand=self.RecScrol.set)
self.RecvList.bind("<Button-1>", self.KeepSelection)
self.QuitButton = tk.Button(window,
text = 'Exit',
command = QuitBtn_Click)
self.QuitButton.grid(row = 14, column = 12)
window.protocol("WM_DELETE_WINDOW", QuitBtn_Click)
def KeepSelection(self, event):
lb = event.widget
selection = lb.curselection()
value = lb.get(selection[0])
print "%s" % value
selected = int(value)
self.lb.select_set(selected)
def RecvBtn_Click(self, event=None):
global RecvQueue, Counter
try:
info = RecvQueue.get(0)
info_split = info.split()
if self.RecvList.size() < len(mydict):
#for _i in mydict.keys():
for _i in sorted(mydict.keys()):
self.RecvList.insert("end",'%06X : %3d %02X %02X %02X %02X %02X %02X %02X %02X %8.1F %8d ' %\
(int(_i,16),
mydict[_i].dlc,
int(mydict[_i].hdata0, 16),
int(mydict[_i].hdata1, 16),
int(mydict[_i].hdata2, 16),
int(mydict[_i].hdata3, 16),
int(mydict[_i].hdata4, 16),
int(mydict[_i].hdata5, 16),
int(mydict[_i].hdata6, 16),
int(mydict[_i].hdata7, 16),
mydict[_i].PCANperiod,
mydict[_i].COUNT))
else:
for i, listbox_entry in enumerate(self.RecvList.get(0, "end")):
list_split = listbox_entry.split()
if list_split[0] == info_split[0]:
pos = self.RecScrol.get()
self.RecvList.delete(i)
self.RecvList.insert(i, info)
self.RecvList.yview_moveto(pos[0])
else:
continue
# self.RecvList.insert("end", info)
self.window.after(20, self.RecvBtn_Click)
Counter += 1
if Counter > 25:
with RecvQueue.mutex:
RecvQueue.queue.clear()
Counter = 0
except Queue.Empty:
self.window.after(50, self.RecvBtn_Click)
def QuitBtn_Click(event=None):
global terminate
global proc
terminate = 1
try:
if terminate == 1:
os.kill(proc.pid, 0)
proc.kill()
root.destroy()
except OSError, e:
print "terminated Gracefully"
# ***************************************************************
#
# ************************ Script Start ************************
#
# ***************************************************************
if __name__ == "__main__":
try:
proc = Popen(("receivetest -f=%s" % pcanbus).split(), stdout = PIPE)
except OSError:
print "ERROR ESTABLISHING CONNECTION"
DataStream = ThreadedTask(RecvQueue)
DataStream.daemon = True
DataStream.start()
root = tk.Tk()
myapp = application(root)
root.mainloop()
# --------------------------------------------------------------
你可以看到我想要做的事情。我的选择方法不起作用,因为索引被删除,因此无法映射到。
预期的数据:
12106694.169 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12106694.638 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12106695.107 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12106695.577 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12106794.137 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12106794.649 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12106795.118 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12106795.587 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12106894.147 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12106894.617 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12106895.86 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12106895.598 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12106994.158 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12106994.627 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12106995.97 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12106995.566 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12107094.169 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12107094.638 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12107095.107 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12107095.619 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12107194.179 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12107194.649 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12107195.118 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12107195.587 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12107294.147 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12107294.659 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12107295.129 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12107295.598 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12107394.158 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12107394.627 receivetest: m s 0x0000069e 8 00 1f be 0f ea 0a 58 1a
12107395.97 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
12107395.566 receivetest: m s 0x00000690 8 00 20 06 0f ea 0a 58 1a
12107494.254 receivetest: m s 0x0000069a 8 00 00 00 00 00 0a 58 1a
12107494.766 receivetest: m s 0x00000690 8 00 20 06 00 00 0a 58 1a
12107495.235 receivetest: m s 0x00000691 8 01 1e 6e 4b 50 5a 06 00
12107495.662 receivetest: m s 0x00000663 8 2f 00 62 02 00 0a 58 1a
答案 0 :(得分:2)
您所要做的就是获取突出显示项目的索引,替换数据,然后重新选择突出显示的项目。如果每个项目都是唯一的,那么这个过程非常简单。
如果这些项目不是唯一的,那么它仍然是可能的。您需要做的就是保存每个所选项目的索引。删除某些内容时,请记住已删除项目的索引。恢复选择时,删除项目上方项目的索引将保持不变,删除项目下方项目的索引需要减去一项(即:如果删除项目#5和#10,则选择删除你需要重新选择#9)
以下是您知道列表框中的项目是唯一的解决方案。
首先,这会按值保存所选的所有内容:
def saveSelection(lb):
selection = []
for i in lb.curselection():
selection.append(lb.get(i))
return selection
稍后,您可以使用以下方法恢复所选项目:
def restoreSelection(lb, selectedItems):
lb.selection_clear(0, "end")
items = lb.get(0, "end")
for item in selectedItems:
if item in items:
index = items.index(item)
lb.selection_set(index)