绘图代码无法快速取值绘制稳定线条及motion()参数错误求助
Hey there! Let's tackle your two issues one by one—first that annoying error, then getting smooth, stable lines with faster mouse coordinate capture.
Fix the motion() takes exactly 1 argument (0 given) Error
This error happens because most GUI frameworks (like Tkinter, a common choice for simple drawing apps) automatically pass an event object to mouse motion callback functions. Your motion() function isn’t set up to accept this required parameter. Here’s how to fix it:
Update your
motion()function to accept the event argument:def motion(event): # Access mouse coordinates directly from the event object current_x = event.x current_y = event.y # Add your drawing logic hereBind the motion event correctly (don’t call
motion()directly—let the framework handle passing the event):# Example with Tkinter Canvas canvas.bind('<Motion>', motion) # If you only want to track movement while drawing (left mouse button pressed), use: # canvas.bind('<B1-Motion>', motion)
Get Faster Mouse Coordinates & Draw Stable Lines
Slow, jittery lines usually come from redundant redraws or inefficient coordinate handling. Try these optimizations:
1. Draw Incremental Line Segments (Not Full Redraws)
Instead of clearing and redrawing the entire canvas every time, draw small line segments between consecutive mouse positions. This cuts down on processing time drastically. Here’s an example:
# Track the previous mouse position to connect lines prev_x = None prev_y = None def motion(event): global prev_x, prev_y current_x = event.x current_y = event.y # Only draw if we have a previous position to connect to if prev_x is not None and prev_y is not None: canvas.create_line(prev_x, prev_y, current_x, current_y, width=2, fill='black') # Update the previous position for the next frame prev_x = current_x prev_y = current_y
2. Filter Unnecessary Motion Events
If you’re tracking all mouse movement (even when not drawing), switch to <B1-Motion> instead of <Motion> (this triggers events only when the left mouse button is pressed). This reduces the number of event calls and speeds up coordinate capture.
3. Add a Coordinate Buffer for Smoothing
For extra-smooth lines, collect a small buffer of recent coordinates and draw between averaged or buffered points. This reduces jitter from fast mouse movement:
coordinate_buffer = [] BUFFER_SIZE = 3 # Adjust based on how smooth you want lines to be def motion(event): global coordinate_buffer coordinate_buffer.append((event.x, event.y)) # Keep the buffer at a fixed size if len(coordinate_buffer) > BUFFER_SIZE: coordinate_buffer.pop(0) # Draw a line between the first and last buffered points if len(coordinate_buffer) >= 2: x1, y1 = coordinate_buffer[0] x2, y2 = coordinate_buffer[-1] canvas.create_line(x1, y1, x2, y2, width=2, fill='black')
4. Optimize Canvas Performance
- For Tkinter canvases, disable extra borders with
canvas = Canvas(root, bd=0, highlightthickness=0)to reduce rendering overhead. - If using other libraries (like Pygame), use hardware acceleration or surface blitting instead of individual line draws for faster rendering.
内容的提问来源于stack exchange,提问作者C. Havlek

