1

When i try clicking on the left listbox, it only highlights in blue the right one. How do i make it highlight both listbox's?

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 Answer 1

1

You can set exportselection argument to False when you define your listbox, so that it can have multiple items selected. So your code would look something like this:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.