Listbox with synchronized selection

Asked

Viewed 29 times

-1

Hello! I am studying about Tkinter and I would like to know if it has how to make the two Listbox that I create, have synchronized selection, and also if it is possible to do the same thing with the scroll bar

Thanks in advance.

from tkinter import *
root = Tk()
root.geometry("500x500")

scroll=Scrollbar(root)  
scroll.place(x=300,y=90)
listbox = Listbox(root,exportselection=0)
listbox.insert(END, "Selecione")
listbox.config(yscrollcommand=scroll.set)
listbox.place(x=50,y=70)

for item in ["1", "2", "3", "4"]:
listbox.insert(END, item)

scroll2=Scrollbar(root)  
scroll2.place(x=330,y=90)
listbox2 = Listbox(root,exportselection=0)
listbox2.pack()
listbox2.insert(END, "Selecione")
listbox2.config(yscrollcommand=scroll2.set)
listbox2.place(x=175,y=70)

for item in ["1", "2", "3", "4"]:
  listbox2.insert(END, item)

root.mainloop()

1 answer

0

Try it here:

from tkinter import *
root = Tk()
root.geometry("500x500")

scroll=Scrollbar(root)  
scroll.place(x=300,y=90)
listbox = Listbox(root,exportselection=0)
listbox.insert(END, "Selecione")
listbox.config(yscrollcommand=scroll.set)
listbox.place(x=50,y=70)

for item in ["1", "2", "3", "4"]:
  listbox.insert(END, item)

scroll2=Scrollbar(root)  
scroll2.place(x=330,y=90)
listbox2 = Listbox(root,exportselection=0)
listbox2.pack()
listbox2.insert(END, "Selecione")
listbox2.config(yscrollcommand=scroll2.set)
listbox2.place(x=175,y=70)

for item in ["1", "2", "3", "4"]:
  listbox2.insert(END, item)

# Criei uma função:

def chamada(event):
  global listbox2
  print(event.x)
  print(event.y)
  listbox2.select_set(0)

# E atribui a função ao evento do clique do mouse na
# primeira listbox:

listbox.bind("<Button-1>",chamada)

root.mainloop()

Then it is only a matter of programming to define where is the position of the click and which you will mark in the other list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.