[Tutor] moving an item from one listbox to another in Python Tkinter

stephen brown sbrown106 at yahoo.co.uk
Fri Apr 24 08:52:57 EDT 2020


Hi,
I am new to python and trying to learn tkinter. Could somebody help me with this 2 problems please Ive been stuck on it for days. I want to move an item from one list box to another but don't want to duplicate the items in the second list box but not sure how to do this. I can move items across but it lets me duplicate as well. Also if somebody could give me some guidance on how I might to this using 'bind' . I have tried this but couldn't understand so ended up using buttons instead. My example code is below.
from tkinter import *
from tkinter import ttk
my_window = Tk()my_listbox_in = Listbox(my_window, height='5')
my_listbox_in.grid(row=0, column=0)
my_listbox_out = Listbox(my_window, height='5')
my_listbox_out.grid(row=0, column=2)my_list = ['1', '2', '4', '6']for item in my_list:
    my_listbox_in.insert(END, item)
def delete():
    my_listbox_in.delete(ANCHOR)
# delete all    my_listbox_in.delete(0,END)
def select():
#   my_label.config(text=my_listbox_in.get(ANCHOR))
    if my_listbox_out.insert(END, my_listbox_in.get(ANCHOR)) not in my_listbox_out:
        my_listbox_out.insert(END, my_listbox_in.get(ANCHOR))
button1 = Button(my_window, text='Delete', command=delete)
button1.grid(row=0, column=1)button2 = Button(my_window, text='select', command=select)
button2.grid(row=1, column=1)
my_label = Label(my_window, text='my_label')
my_label.grid(row=2, column=1)#my_listbox_in.bind('<<ListboxSelect>>', select())mainloop()



More information about the Tutor mailing list