#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2

    Can selections be made from more than 1 listbox?


    Can I display 2 listboxes and make selections from either one
    without impacting selections in the other listbox? In this code
    you'll see that selections in one listbox are cleared as soon as
    a selection is made in the other listbox.
    Thanks.
    Code:
    #goal: find a method to select items from two listboxes
    from tkinter import *
    import tkMessageBox
    
    root = Tk()
    root.geometry('500x400+20+20')
    
    def count_picks():
        """
        show how many items are selected in each listbox
        """
        selected_items = []
    
        count1 = str(len(listbox1.curselection()))
        count2 = str(len(listbox2.curselection()))
        
        title = "Item Counts"
        message = 'You selected ' + count1 + ' items from listbox #1\n and ' + \
            count2 + ' items from listbox #2.'
            
        tkMessageBox.showinfo(title, message)
    
    def clear_selections():
        """
        clear selections from two listboxes
        """
        listbox1.selection_clear(0, END)
        listbox2.selection_clear(0, END)
        
    Btn1 = Button(padx = 8, pady = 6, text = "Count Selections", command = count_picks)
    Btn1.pack()
    Btn1.place(x = 100, y = 320)
    
    Btn2 = Button(padx = 10, pady = 6, text = "Clear Selections", command = clear_selections)
    Btn2.pack()
    Btn2.place(x = 260, y = 320)
    
    Label(root, text="Listbox #1").place(x = 70, y = 30)
    Label(root, text="Listbox #2").place(x = 350, y = 30)
    
    listbox1 = Listbox(root, selectmode=EXTENDED)
    listbox2 = Listbox(root, selectmode=EXTENDED)
    listbox1.pack()
    listbox2.pack()
    
    listbox1.config(width=25, height=15)
    listbox2.config( width=25, height=15)
    
    listbox1.place(x = 30, y = 50)
    listbox2.place(x = 300, y = 50)
    
    for items in ["1   First choice", "2   Second choice", "3   Third choice"]:
        listbox1.insert(END, items)
    
    for items in ["4   Fourth choice", "5   Fifth choice", "6   Sixth choice"]:
        listbox2.insert(END, items)
    
    root.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    When you define your listboxen, add "exportselection=0" to the definition, like so:

    Code:
    listbox1 = Listbox(root, selectmode=EXTENDED, exportselection=0)
    listbox2 = Listbox(root, selectmode=EXTENDED, exportselection=0)
    The side effect of this is that, on X11, selections won't be exported as the X11 selection. This may impact, e.g., the X clipboard, if that matters. Most likely it doesn't.
    Last edited by admoore; August 20th, 2013 at 09:11 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2
    Perfect. There is no clipboard issue. Thank you admoore.
    SOLVED

IMN logo majestic logo threadwatch logo seochat tools logo