Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Xóa đường vẽ từ Canvas Tkinter

Canvas widget vẽ hình dạng, graphics. Vẽ line: create_line(x,y,x1,y1, **options). Có line đơn và nét đứt (dash).

Để xóa line đã tạo, dùng phương thức delete().

Ví dụ

from tkinter import *

win = Tk()
win.geometry("700x350")

def on_click():
    canvas.delete(line)

canvas = Canvas(win, width=500, height=300)
canvas.pack()
line = canvas.create_line(100, 200, 200, 35, fill="red", width=10)
Button(win, text="Delete Shape", command=on_click).pack()
win.mainloop()

Kết quả

Canvas có line đỏ. Click "Delete Shape" → line biến mất.

Xóa đường vẽ từ Canvas Tkinter

Lưu ý: canvas.delete(item_id) xóa 1 item. canvas.delete("all") xóa tất cả. Item ID trả về từ create_* methods.