0

I have fixed this by adding 'exportselection=False' to the listbox line. but if somebody could explain why the [0] is needed in the line 'my_listbox_1.curselection()[0]'. I would be grateful, if not no worries.

from tkinter import *
from tkinter import ttk
my_window = Tk()

frame_for_list1 = Frame(my_window, padx=20, pady=20)
frame_for_list1.grid(row=0, column=0)

frame_for_list2 = Frame(my_window, padx=20, pady=20)
frame_for_list2.grid(row=0, column=1)

my_listbox_1 = Listbox(frame_for_list1, exportselection=False, height='5', width='5')
my_listbox_1.grid(row=0, column=0)

my_listbox_2 = Listbox(frame_for_list2, exportselection=False, height='5', width='5')
my_listbox_2.grid(row=0, column=0)

my_list_1 = ['1', '2', '4', '6']
my_list_2 = ['11', '21', '41', '61']

for item1 in my_list_1:
    my_listbox_1.insert(END, item1)

for item2 in my_list_2:
    my_listbox_2.insert(END, item2)


def select_list1(event=None):
    my_label_list1 = Label(frame_for_list1, text=my_listbox_1.get(my_listbox_1.curselection()[0]))
    my_label_list1.grid(row=1, column=0, pady=10)


my_listbox_1.bind('<<ListboxSelect>>', select_list1)


def select_list2(event=None):
    my_label_list2 = Label(frame_for_list2, text=my_listbox_2.get(my_listbox_2.curselection()[0]))
    my_label_list2.grid(row=1, column=2, pady=10)


my_listbox_2.bind('<<ListboxSelect>>', select_list2)

mainloop()
Sid
  • 153
  • 3
  • 15
  • Are you aware that you are binding the functions to left and right arrow keys instead of mouse buttons? – TheFluffDragon9 Apr 27 '20 at 12:45
  • _"I cant seem to select and display seprately from both lists"_ - does the following link help answer that part of the question? https://stackoverflow.com/questions/756662/how-to-select-at-the-same-time-from-two-listbox – Bryan Oakley Apr 27 '20 at 13:13
  • sorry my fault wrong bit of code... will try again – Sid Apr 27 '20 at 13:57
  • sorry I have re-posted this. The code posted was for a different problem. Thanks for the reply – Sid Apr 27 '20 at 14:04
  • Don't re-post. You can [edit] this question to have the correct code. – Bryan Oakley Apr 27 '20 at 15:22
  • I have edited and corrected the code - will know from now on,Sorry about the re-posting – Sid Apr 27 '20 at 15:52

0 Answers0