在 Tkinter 中用鼠标实时绘图
Real time drawing with mouse in Tkinter
我对在 Tkinter 中绘图相当得心应手,但您只有在松开鼠标按钮后才能看到形状。拖动鼠标时如何看到形状?我已经完成了下面代码的一半,但是如果你 运行 它你会看到它不会更新以删除由运动功能制作的每张图。
from tkinter import *
window = Tk()
canvas = Canvas(bg='black')
canvas.pack()
def locate_xy(event):
global current_x, current_y
current_x, current_y = event.x, event.y
def draw_circle(event):
global current_x, current_y
canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
current_x, current_y = event.x, event.y
def update_circle(event):
global current_x, current_y
canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
canvas.bind('<ButtonPress-1>', locate_xy)
canvas.bind('<ButtonRelease-1>', draw_circle)
canvas.bind('<B1-Motion>', update_circle)
window.mainloop()
您已经在通过事件 <B1-Motion>
按下左键来跟踪鼠标移动,因此只需在此处添加绘图:
from tkinter import *
window = Tk()
canvas = Canvas(bg='black')
canvas.pack()
def draw_line(event):
x, y = event.x, event.y
canvas.create_oval((x-2, y-2, x+2, y+2), outline='white')
canvas.bind('<B1-Motion>', draw_line)
window.mainloop()
您只需在 locate_xy()
内创建椭圆并更新其在 update_circle()
内的坐标。 <ButtonRelease-1>
的绑定不是必需的:
def locate_xy(event):
global current_x, current_y, current_item
current_x, current_y = event.x, event.y
# create the oval item and save its ID
current_item = canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
def update_circle(event):
# update coords of the oval item
canvas.coords(current_item, (current_x, current_y, event.x, event.y))
canvas.bind('<ButtonPress-1>', locate_xy)
canvas.bind('<B1-Motion>', update_circle)
我对在 Tkinter 中绘图相当得心应手,但您只有在松开鼠标按钮后才能看到形状。拖动鼠标时如何看到形状?我已经完成了下面代码的一半,但是如果你 运行 它你会看到它不会更新以删除由运动功能制作的每张图。
from tkinter import *
window = Tk()
canvas = Canvas(bg='black')
canvas.pack()
def locate_xy(event):
global current_x, current_y
current_x, current_y = event.x, event.y
def draw_circle(event):
global current_x, current_y
canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
current_x, current_y = event.x, event.y
def update_circle(event):
global current_x, current_y
canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
canvas.bind('<ButtonPress-1>', locate_xy)
canvas.bind('<ButtonRelease-1>', draw_circle)
canvas.bind('<B1-Motion>', update_circle)
window.mainloop()
您已经在通过事件 <B1-Motion>
按下左键来跟踪鼠标移动,因此只需在此处添加绘图:
from tkinter import *
window = Tk()
canvas = Canvas(bg='black')
canvas.pack()
def draw_line(event):
x, y = event.x, event.y
canvas.create_oval((x-2, y-2, x+2, y+2), outline='white')
canvas.bind('<B1-Motion>', draw_line)
window.mainloop()
您只需在 locate_xy()
内创建椭圆并更新其在 update_circle()
内的坐标。 <ButtonRelease-1>
的绑定不是必需的:
def locate_xy(event):
global current_x, current_y, current_item
current_x, current_y = event.x, event.y
# create the oval item and save its ID
current_item = canvas.create_oval((current_x, current_y, event.x, event.y), outline='white')
def update_circle(event):
# update coords of the oval item
canvas.coords(current_item, (current_x, current_y, event.x, event.y))
canvas.bind('<ButtonPress-1>', locate_xy)
canvas.bind('<B1-Motion>', update_circle)