从一个列表框移动到另一个列表框

时间:2017-04-26 06:49:51

标签: python tkinter listbox

我一直在尝试创建一个两个列表框小部件来选择,添加和删除项目。

像这样:http://www.contextures.com/images/listboxmoveuserform01.png

到目前为止,它按预期工作,但我希望它不添加已存在于其他列表框中的列表项。

我的代码到目前为止,对于按钮命令:

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)

目前,当选择1,2,3并按两次按钮时,它会这样做:

List1    List2    List1    List2
1        1        1        1 
2        2        2        2
3        3        3        3 
4                 4        1
5                 5        2
                           3

我希望它不再重复相同的元素,如果它已经存在于另一个列表框中。

1 个答案:

答案 0 :(得分:1)

您可以使用list对象和listvariable参数的强大功能! 这里的主要逻辑 - 通过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()

链接: