Binding mouse double click in Tkinter

TkinterServer Side ProgrammingProgramming

Let us suppose that for a particular application, we want to bind the mouse double-click so that it performs some event or operation. We can use the bind(‘<Double-Button-1>’, handler) or bind(‘<Double-Button-2>’, handler) methods to bind the mouse Left or Right Buttons with a handler or a callback function.

Example

In this example, we will create an application that contains a button. When we double click the button, it will open a popup window.

#Import required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("750x250")
#Define a function
def handler(e):
   top= Toplevel(win)
   top.geometry("600x200")
   Label(top, text= "Hey There!", font= ('Helvetica 15 bold')).pack(pady=30)

#Define a Label in Main window
Label(win, text= "Double Click to Open the Popup",font=('Helvetica 15 underline')).pack(pady=30)

#Create a Button
ttk.Button(win, text= "Click", command=lambda:handler).pack(pady=20)

#Bind the Double Click with the Handler
win.bind('<Double-Button-1>', handler)
win.mainloop()

Output

Now, run the above code to display the window.

Double-click the button "Click" to open a popup window.

raja
Updated on 03-May-2021 07:44:15

Advertisements