0

I have 2 listboxes (which are connected so that items can move from one to the other) and at the end I would like to get all the entries in the second listbox by using a 'Ok' button (or simply closing the frame). I could add/remove values to a list every time an item is selected (as shown in the commented section of the code below) but I would rather have a single line of code along the lines of [master.selected.get(idx) for idx in master.selected.curselection()] in the close function but I am unable to get it working.

Code:

def measurementPopup(self,master):
    self.chargeCarrier = StringVar()
    self.massModifiers = StringVar()
    self.chargeCarrier.set("[M+xH]")

    def onselect1(evt):
        w = evt.widget
        index = int(w.curselection()[0])
        value = w.get(index)
        # My Dirty fix -> Here I could enter the selected value to a buffer list (to be returned in the ok function).
        master.selected.insert(END,value)
        master.avail.delete(index)

    def onselect2(evt):
        w = evt.widget
        index = int(w.curselection()[0])
        value = w.get(index)
        # My Dirty fix -> Here I could remove the selected value from a buffer list (to be returned in the ok function).
        master.selected.delete(index)
        master.avail.insert(END,value)

    def close(self):
        # Here I would return the buffer list and close the window
        master.measurementWindow = 0
        top.destroy()

    if master.measurementWindow == 1:
        return

    master.measurementWindow = 1
    top = self.top = Toplevel()
    top.protocol( "WM_DELETE_WINDOW", lambda: close(self))
    self.charge = Label(top, text = "Charge", width = 10)
    self.charge.grid(row = 0, column = 0, sticky = W)
    self.min = Label(top, text = "Min", width = 5)
    self.min.grid(row=0, column = 1, sticky = W)
    self.minCharge = Spinbox(top, from_= 1, to = 3, width = 5)
    self.minCharge.grid(row = 0, column = 2, sticky = W)
    self.max = Label(top, text = "Max", width = 5)
    self.max.grid(row = 0, column = 3, sticky = W)                  
    self.maxCharge = Spinbox(top, from_ = 1, to=3, width=5)
    self.maxCharge.grid(row = 0, column = 4, sticky = W)
    self.chargeCarrier = OptionMenu(top, self.chargeCarrier, "[M+xH]", "[M+xNa]")
    self.chargeCarrier.grid(row = 0, column = 5, sticky = W)
    self.availMass = Label(top, text = "Available")
    self.availMass.grid(row = 1, column = 1, sticky = W)
    self.selectMass = Label(top, text = "Selected")
    self.selectMass.grid(row = 1, column = 3, sticky = W)
    self.massMod = Label(top, text = "Mass Mods")
    self.massMod.grid(row = 2, column = 0, sticky = W)
    self.avail = Listbox(top)
    for i in UNITS:
        if BLOCKS[i]['available'] == 1:
            self.avail.insert(END,BLOCKS[i]['human_readable_name'])
    self.avail.grid(row = 2, column = 1, columnspan = 2, sticky = W)
    self.avail.bind('<<ListboxSelect>>',onselect1)
    self.selected = Listbox(top)
    self.selected.grid(row = 2, column = 3, columnspan = 2, sticky = W)
    self.selected.bind('<<ListboxSelect>>',onselect2)
    self.ok = Button(top,text = 'Ok',command = lambda: close(self))
    self.ok.grid(row = 3, column = 0, sticky = W)

I have tried to use the following small snippet in the close function:

        values = [master.selected.get(idx) for idx in master.selected.curselection()]
        print ', '.join(values)

However, the for segment doesn't return anything. I would expect that this is due to the fact that nothing is actually selected but that I would need something opposite, along the lines of master.selected.allitems() (if it exists and if I understand it correctly).

Summary

How would one get all the items in 1 specific listbox?

1 Answer 1

3

The .get() function for the Listbox widget allows you to specify a range of items, which can be specified as 0 to END to return a tuple of all the items.

Example:

from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")

def close():
    global l, root
    items = l.get(0, END)
    print(items)
    root.destroy()

b = Button(root, text = "OK", command = close).pack()
root.mainloop()

I hope this helps, if it's not what you were looking for let me know in a comment and I can try expand my answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.