我對在 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()
uj5u.com熱心網友回復:
您只需要在里面創建橢圓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)
uj5u.com熱心網友回復:
您已經在 event 按下左鍵的情況下跟蹤滑鼠移動<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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419065.html
標籤:
