Reputation: 135
I have been trying to make a two list box widget to select, add and remove items from one to another.
Like this: http://www.contextures.com/images/listboxmoveuserform01.png
So far it works as expected, but I would like for it to NOT add list items which are already present in the other listbox.
My code so far, for the button commands:
def all_to_right(self):
self.Scrolledlistbox2.delete(0,END)
for item in self.updatescrolledlistbox():
self.Scrolledlistbox2.insert(END,item)
def all_to_left(self):
self.Scrolledlistbox2.delete(0,END)
def one_to_right(self):
index=self.Scrolledlistbox1.curselection()
for x in index:
d=self.Scrolledlistbox1.get(x)
self.Scrolledlistbox2.insert(END,d)
def one_to_left(self):
index=self.Scrolledlistbox2.curselection()
print(index)
for x in index:
self.Scrolledlistbox2.delete(x,END)
Currently it does when 1,2,3 are selected and button clicked twice:
List1 List2 List1 List2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 1
5 5 2
3
I want it not repeat the same elements again, if it already exists in the other list box.
Upvotes: 1
Views: 3193
Reputation: 4482
You can use power of the list
object and listvariable
parameter!
Main logic here - grab and concatenate lists and populate listboxes via StringVar.
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.resizable(width=False, height=False)
# list variables
self.list_var1 = tk.StringVar()
self.list_var2 = tk.StringVar()
# main frame
self.main_frame = tk.Frame(self)
self.listbox1 = tk.Listbox(self.main_frame, listvariable=self.list_var1, selectmode='multiple')
self.listbox2 = tk.Listbox(self.main_frame, listvariable=self.list_var2, selectmode='multiple')
# little button frame
self.button_frame = tk.Frame(self.main_frame)
self.all_to_right_button = tk.Button(self.button_frame, text='>>', command=self.move_to_right)
self.one_to_right_button = tk.Button(self.button_frame, text='>', command=lambda: self.move_to_right(True))
self.one_to_left_button = tk.Button(self.button_frame, text='<', command=lambda: self.move_to_left(True))
self.all_to_left_button = tk.Button(self.button_frame, text='<<', command=self.move_to_left)
# packing
self.all_to_right_button.pack()
self.one_to_right_button.pack()
self.one_to_left_button.pack()
self.all_to_left_button.pack()
self.listbox1.pack(side='left', anchor='w')
self.button_frame.pack(side='left')
self.listbox2.pack(side='right', anchor='e')
self.main_frame.pack()
# insert default values
self.init_default_values()
def init_default_values(self):
self.list_var1.set(value=[1, 2, 3, 4, 5])
self.list_var2.set(value=[1, 2, 3])
# you can test with literals
# self.list_var1.set(value=['A', 'B', 'C', 'D', 'E'])
# self.list_var2.set(value=['A', 'B', 'C'])
def move_to_right(self, only_one_item=False):
if self.listbox1.curselection() == ():
return
# get tuple of selected indices
if only_one_item:
selection = (self.listbox1.curselection()[0],)
else:
selection = self.listbox1.curselection()
# left all/selected values
left_value_list = [line.strip(' \'') for line in self.list_var1.get()[1:-1].split(',')]
left_selected_list = [left_value_list[index] for index in selection]
# values from right side
right_value_list = [line.strip(' \'') for line in self.list_var2.get()[1:-1].split(',')]
# merge w/o duplicates
result_list = sorted(list(set(right_value_list + left_selected_list)))
self.list_var2.set(value=result_list)
def move_to_left(self, only_one_item=False):
if self.listbox2.curselection() == ():
return
# get tuple of selected indices
if only_one_item:
selection = (self.listbox2.curselection()[0],)
else:
selection = self.listbox2.curselection()
# right all/selected values
right_value_list = [line.strip(' \'') for line in self.list_var2.get()[1:-1].split(',')]
right_selected_list = [right_value_list[index] for index in selection]
# values from left side
left_value_list = [line.strip(' \'') for line in self.list_var1.get()[1:-1].split(',')]
# merge w/o duplicates
result_list = sorted(list(set(left_value_list + right_selected_list)))
self.list_var1.set(value=result_list)
app = App()
app.mainloop()
Links:
Upvotes: 1