Binding Function with double click with Tkinter ListBox

Prerequisites: Python GUI – tkinter, Python | Binding function in Tkinter

Tkinter in Python is GUI (Graphical User Interface) module which is widely used for creating desktop applications. It provides various basic widgets to build a GUI program.

To bind Double click with Listbox we use Binding functions in Python and then we execute the required actions based on the item selected in Listbox.
Below is the implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

from tkinter import *
   
def go(event):
    cs = Lb.curselection()
      
    # Updating label text to selected option
    w.config(text=Lb.get(cs))
      
    # Setting Background Colour
    for list in cs:
          
        if list == 0:
            top.configure(background='red')
        elif list == 1:
            top.configure(background='green')
        elif list == 2:
            top.configure(background='yellow')
        elif list == 3:
            top.configure(background='white')
   
   
top = Tk()
top.geometry('250x275')
top.title('Double Click')
   
# Creating Listbox
Lb = Listbox(top, height=6)
# Inserting items in Listbox
Lb.insert(0, 'Red')
Lb.insert(1, 'Green')
Lb.insert(2, 'Yellow')
Lb.insert(3, 'White')
   
# Binding double click with left mouse
# button with go function
Lb.bind('<Double-1>', go)
Lb.pack()
   
# Creating Edit box to show selected option
w = Label(top, text='Default')
w.pack()
top.mainloop()

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.