Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw a line following mouse coordinates with tkinter?
To draw a line following mouse coordinates in tkinter, we need to capture mouse click coordinates and draw lines between consecutive points. This creates an interactive drawing experience where each click connects to the previous point.
Steps
Import the tkinter library and create an instance of tkinter frame.
Set the size of the frame using geometry method.
Create a user-defined method "draw_line" to capture the x and y coordinates of each mouse click. Then, use the create_line() method of Canvas to draw a line between two consecutive points.
Bind the left-click of the mouse with the draw_line method.
Finally, run the mainloop of the application window.
Example
Here's a complete example that creates a drawing canvas where clicking connects points with lines ?
# Import the library
import tkinter as tk
# Create an instance of tkinter
win = tk.Tk()
win.title("Line Drawing with Mouse")
# Window size
win.geometry("700x400")
# Method to draw line between two consecutive points
def draw_line(e):
x, y = e.x, e.y
if canvas.old_coords:
x1, y1 = canvas.old_coords
canvas.create_line(x, y, x1, y1, width=3, fill='blue')
canvas.old_coords = x, y
# Create canvas
canvas = tk.Canvas(win, width=700, height=400, bg='white')
canvas.pack()
canvas.old_coords = None
# Bind the left button of the mouse
win.bind('<Button-1>', draw_line)
win.mainloop()
How It Works
The draw_line function captures mouse coordinates when you click. It stores the current position in canvas.old_coords and draws a line from the previous position to the current one using create_line().
Enhanced Version with Reset Feature
Here's an improved version that includes a reset button to clear the canvas ?
import tkinter as tk
def draw_line(e):
x, y = e.x, e.y
if canvas.old_coords:
x1, y1 = canvas.old_coords
canvas.create_line(x, y, x1, y1, width=3, fill='red', smooth=True)
canvas.old_coords = x, y
def reset_canvas():
canvas.delete("all")
canvas.old_coords = None
# Create main window
win = tk.Tk()
win.title("Interactive Line Drawing")
win.geometry("700x450")
# Create canvas
canvas = tk.Canvas(win, width=700, height=400, bg='lightgray')
canvas.pack(pady=10)
canvas.old_coords = None
# Add reset button
reset_btn = tk.Button(win, text="Clear Canvas", command=reset_canvas,
bg='orange', font=('Arial', 12))
reset_btn.pack()
# Bind mouse click
win.bind('<Button-1>', draw_line)
win.mainloop()
Key Features
Mouse Tracking: Captures x, y coordinates on each left-click
Line Drawing: Connects consecutive points with smooth lines
State Management: Stores previous coordinates in
canvas.old_coordsInteractive Canvas: Real-time drawing as you click different points
Conclusion
This technique creates an interactive drawing application by binding mouse events to a canvas. Each click stores coordinates and draws lines between consecutive points, creating connected line drawings.
