How to bind a Tkinter event to the left mouse button being held down?

To bind a Tkinter event to the left mouse button being held down, you need to use the <B1-Motion> event. This event triggers when the mouse moves while the left button is pressed down.

Key Events for Mouse Button Handling

Here are the essential mouse events for detecting left button interactions ?

  • <Button-1> − Left mouse button is pressed down

  • <B1-Motion> − Mouse moves while left button is held down

  • <ButtonRelease-1> − Left mouse button is released

Example

Here's a complete example that demonstrates binding events to detect when the left mouse button is held down ?

# Import required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Define the geometry of the window
win.geometry("750x250")

# Define event handlers
def on_drag(event):
    print(f"Dragging at position: ({event.x}, {event.y})")

def on_release(event):
    print("Left button released!")

def on_press(event):
    print("Left button pressed!")

# Define a Label in main window
Label(win, text="Click and drag with the Left Mouse Button", 
      font='Helvetica 15 underline').pack(pady=30)

# Bind the mouse events with the handlers
win.bind('<Button-1>', on_press)           # Button press
win.bind('<B1-Motion>', on_drag)           # Button held down + motion
win.bind('<ButtonRelease-1>', on_release)  # Button release

win.mainloop()

How It Works

The event binding works as follows ?

  • <Button-1> detects when the left mouse button is first pressed

  • <B1-Motion> continuously triggers while moving the mouse with the left button held down

  • <ButtonRelease-1> detects when the left button is released

Output

When you run the code and interact with the window, the console output will look like this ?

Left button pressed!
Dragging at position: (150, 100)
Dragging at position: (152, 102)
Dragging at position: (155, 105)
Dragging at position: (158, 108)
Left button released!

Practical Use Cases

This mouse event binding is commonly used for ?

  • Creating drawing applications where users can draw by dragging

  • Implementing drag-and-drop functionality

  • Building interactive games that require mouse tracking

  • Creating resizable or movable GUI components

Conclusion

Use <B1-Motion> to detect when the left mouse button is held down and moved. Combine it with <Button-1> and <ButtonRelease-1> for complete mouse interaction control in your Tkinter applications.

Updated on: 2026-03-26T18:30:21+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements