如何使2个具有多个选择的Tkinter(python)列表框保持最新?

2024-09-30 01:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python中的Tkinter创建了一个对话框。我有两个列表框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 box中的所有选择都将取消选中。对于刷新方法,同样的事情也会发生

似乎我只能在其中一个列表框中显示所选的项目-正在单击的列表框

感谢您的帮助

bobls


Tags: 项目inselffordefselectevtset

热门问题