[Tkinter-discuss] Listbox selection issue

Michael Lange klappnase at web.de
Mon Apr 19 17:10:01 EDT 2004


On Mon, 19 Apr 2004 10:02:38 -0300
"Batista, Facundo" <FBatista at uniFON.com.ar> wrote:

> #- ###########file test.py############################################
> #- #!/usr/bin/env python
> #- from Tkinter import *
> #- 
> #- class Test(Tk):
> #-     def __init__(self):
> #-         Tk.__init__(self)
> #-         l = Listbox(self)
> #-         l.pack()
> #-         l.insert('end', 'line 1')
> #-         l.insert('end', 'line 2')
> #-         l.insert('end', 'line 3')
> #-         l.bind('<1>', self.dummy)
> #-         l.bind('<Double-Button-1>', self.test)
> #-         
> #-     def dummy(self, event):
> #-         return 'break'
> #-     
> #-     def test(self, event):
> #-         print 'hello'
> #- 
> #- if __name__ == '__main__':
> #-     t = Test()
> #-     t.mainloop()
> #- 
> #- #############################################################

> The issue here is that when you click in an item, it does not get colored
> (or remarked, or selected, or however it says).
>
> Why that happens?
> 

That's because selecting an item is done by Button-1 events normally and this is prevented here
by the "return 'break'" statement (I thought that's what you wanted, or did I misunderstand
something?). If you want the item selected on Double-Button-1 events you need of course do
this in the "test()" method here. I wrote a method a while ago that returns the index of the
listbox item that was clicked on, which might be helpful, see example below:

########file test.py##########################################
#!/usr/bin/env python
from Tkinter import *

class Test(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.listbox = l = Listbox(self)
        l.pack()
        l.insert('end', 'line 1')
        l.insert('end', 'line 2')
        l.insert('end', 'line 3')
        l.bind('<1>', self.dummy)
        l.bind('<Double-Button-1>', self.test)
        
    def dummy(self, event):
        return 'break'
    
    def test(self, event):
        i = self.where(event)
        if i > -1:
            self.listbox.select_clear(0, 'end')
            self.listbox.select_set(i)
            print 'item double-clicked: ' + self.listbox.get(i)
        
    def where(self, event):
        '''Returns the index of the listbox where the event occured.'''
        # where the corner of the listbox is relative to the screen:
        x_org = self.listbox.winfo_rootx()
        y_org = self.listbox.winfo_rooty()
        # where the pointer is relative to the listbox widget:
        x = event.x_root - x_org
        y = event.y_root - y_org
        i = self.listbox.index('@' + str(x) + ',' + str(y))
        b = self.listbox.bbox(i)
        if not b or y > b[1] + b[3]:
            # listbox is empty or event occured below the last item
            return -1
        else:
            return i

if __name__ == '__main__':
    t = Test()
    t.mainloop()
###################################################################

Is it something like this, what you had in mind or did I get something wrong
(unfortunately I already deleted the first posts from this thread)?

Best regards

Michael



More information about the Tkinter-discuss mailing list