Home > Software engineering >  Disable mouse double click event in tkinter
Disable mouse double click event in tkinter

Time:12-08

I am thinking about disabling the mouse double click event after one event. If i double click on an item from the list box the event disables until i press the Enable Double click button. How to archive this?

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()

# Creating Enable button to enable double clicking
enable_btn = Button(top, text = 'Enable Double Click')
enable_btn.pack(pady = 10)

top.mainloop()

CodePudding user response:

To disable double-click, bind to that event and then have your function return the string "break". You can use a variable to trigger that behavior.

Start by defining a flag, and then a function that can set the flag:

double_click_enabled = False
def enable_double_clicking(enable):
    global double_click_enabled
    double_click_enabled = enabled

Next, define your button to call this function. In this example I'll show two buttons for enabling and disabling:

enable_btn = Button(top, text = 'Enable Double Click', command=lambda: enable_double_clicking(True))
disable_btn = Button(top, text = 'Disable Double Click', command=lambda: enable_double_clicking(False))

Finally, check for the flag at the top of your go function. If double-clicking is disabled, return the string "break" which prevents the result of the function from executing and also disables any default behavior.

def go(event):
    if not double_click_enabled:
        return "break"
    ...
  • Related