Python Tkinter Listbox没有突出显示

时间:2016-05-18 19:20:21

标签: tkinter

当我尝试点击左侧列表框时,它只会以蓝色突出显示右侧列表框。如何让它突出显示两个列表框?

from Tkinter import *
root=Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )


mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
   mylist.insert(END, "This is line number " + str(line))
mylist.pack( side = RIGHT, fill = BOTH )

mylist2 = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
   mylist2.insert(END, "This is line number " + str(line))
mylist2.pack( side = RIGHT, fill = BOTH )

def scroll_bar(*args):
    mylist.yview(*args)
    mylist2.yview(*args)
scrollbar.config( command = scroll_bar )
def side_highlight(e):
    select_number= mylist2.curselection() #gets where in listbox is selected
    mylist.selection_set(select_number)

mylist2.bind('<<ListboxSelect>>', side_highlight)

root.mainloop()

1 个答案:

答案 0 :(得分:1)

您可以在定义列表框时将exportselection参数设置为False,以便可以选择多个项目。所以你的代码看起来像这样:

from Tkinter import *
root=Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )


mylist = Listbox(root, yscrollcommand = scrollbar.set, exportselection = False)
for line in range(100):
   mylist.insert(END, "This is line number " + str(line))
mylist.pack( side = RIGHT, fill = BOTH )

mylist2 = Listbox(root, yscrollcommand = scrollbar.set, exportselection = False)
for line in range(100):
   mylist2.insert(END, "This is line number " + str(line))
mylist2.pack( side = RIGHT, fill = BOTH)

def scroll_bar(*args):
    mylist.yview(*args)
    mylist2.yview(*args)
scrollbar.config( command = scroll_bar )
def side_highlight(e):
    select_number= mylist2.curselection() #gets where in listbox is selected
    mylist.selection_clear(0, END)      # Needs to clear all previous elements
    mylist.selection_set(select_number)

mylist2.bind('<<ListboxSelect>>', side_highlight)

root.mainloop()