- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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, we can take the following steps −
Create an instance of tkinter frame.
Set the size of the frame using win.geometry method.
Define an event handler "handler1" to print a statement when the mouse is moved with the left button being held down.
Define another event handler "handler2" to print a statement when the mouse button is released.
Use the bind method to bind <B1-Motion> with handler1.
Use the bind method again to bind <ButtonRelease-1> with hander2.
Finally, run the mainloop of the application window.
Example
# Import required libraries from tkinter import * # Create an instance of tkinter frame win = Tk() # Define the geometry of the window win.geometry("750x250") # Define a function def handler1(e): print("You are moving the Mouse with the Left Button Pressed.") def handler2(e): print("Button Released") # Define a Label in Main window Label(win, text="Move the Mouse with the Left Button Pressed", font='Helvetica 15 underline').pack(pady=30) # Bind the Mouse events with the Handler win.bind('<B1-Motion>', handler1) win.bind('<ButtonRelease-1>', handler2) win.mainloop()
Output
When you execute the code, it will show the following screen −
Now, move the mouse with the Left Button pressed and it will show the following output on the console
You are moving the Mouse with the Left Button Pressed. You are moving the Mouse with the Left Button Pressed. You are moving the Mouse with the Left Button Pressed. You are moving the Mouse with the Left Button Pressed. You are moving the Mouse with the Left Button Pressed.
When you release the Left Button of the mouse, it will show the following −
Button Released
- Related Articles
- How to bind a key to a button in Tkinter?
- How to bind a click event to a Canvas in Tkinter?
- How to handle a Button click event in tkinter?
- Mouse event not being triggered on HTML5 canvas? How to solve it?
- How to bind the Enter key to a tkinter window?
- How to bind multiple events with one "bind" in Tkinter?
- How to bind to shift+tab in Tkinter?
- How to bind events to Tkinter Canvas items?
- How to handle bind an event using JavaScript?
- How to bind the Escape key to close a window in Tkinter?
- How to bind the spacebar key to a certain method in Tkinter?
- How to bind all the number keys in Tkinter?
- How to move a Tkinter canvas with Mouse?
- How do I bind the enter key to a function in Tkinter?
- How to create a Tkinter toggle button?
