我在python中使用Tkinter创建了一个对话框。我有2个Listbox wiget是为多个选择设置的。我为每个列表框设置了事件,并使用curselection获取列表中当前选定项的列表。但是,当我单击第二个列表框时,第一个列表框中的项目将不再突出显示。下面是一些代码片段:
创建第一个列表框:
casebox = Listbox(self.window, listvariable=casevalues, selectmode=MULTIPLE, width=10, height=10)
casebox.grid(column=1, row=self.dlgRow, columnspan=1, rowspan=2)
casebox.bind('<<ListboxSelect>>', oncaseselect)
第二个列表框类似:
plotbox = Listbox(self.window, listvariable=plotvalues, selectmode=MULTIPLE, width=30, height=10)
plotbox.grid(column=2, row=self.dlgRow, columnspan=2, rowspan=2)
plotbox.bind('<<ListboxSelect>>', onplotselect)
列表框正确显示,我可以预选项目:
casebox.select_set(1)
casebox.select_set(3)
当我尝试在第二个列表框中预选项目时,它们会被选中,但是第一个框中没有显示选中的任何内容:
plotbox.select_set(1)
plotbox.select_set(3)
事件处理程序是:
def oncaseselect(evt):
w = evt.widget
#self.selectedCaseList = [w.get(i) for i in w.curselection()]
self.selectedCaseList=[]
for i in w.curselection():
self.selectedCaseList.append(i)
refreshPlotList()
refreshCaseList()
def onplotselect(evt):
w = evt.widget
# self.selectedPlotList = [w.get(i) for i in w.curselection()]
self.selectedPlotList=[]
for i in w.curselection():
self.selectedPlotList.append(i)
refreshCaseList()
refreshPlotList()
我添加了刷新方法,以使选择保持更新:
def refreshCaseList():
for item in self.selectedCaseList:
casebox.select_set(item)
def refreshPlotList():
for item in self.selectedPlotList:
plotbox.select_set(pitem)
在没有刷新方法的情况下,一旦我在plotbox中单击,所有case框中的选择都将被取消。对于刷新方法,也会发生同样的事情。
似乎我只能在其中一个列表框中显示选定的项目--单击的列表框。
任何帮助都是非常感谢的。
波布斯
发布于 2022-04-12 18:19:37
您需要在Tkinter中包含exportselection=0
选项
https://stackoverflow.com/questions/59874397
复制相似问题