from tkinter import *
class plant:
def __init__(self):
window = Tk()
window.title("手寫畫板")
self.canvas = Canvas(window,width =600, height = 500,bg = "White")
frame = Frame(window)
btClear=Button(frame, text=' Clear All ',font=('Arial', 15),borderwidth=10
,bg='light blue', command=self.clear).pack()
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
self.canvas.grid(column=0, row=0, sticky='nwes')
self.canvas.bind('<Button-1>', self.xy)
self.canvas.bind('<B1-Motion>', self.line)
self.canvas.pack()
frame.pack()
window.mainloop()
lasty, lastx = 0, 0
def clear(self):
self.canvas.delete('li')
def xy(self,event):
global lasty, lastx
lastx = event.x
lasty = event.y
def line(self,event):
global lasty, lastx
self.canvas.create_line(lastx,lasty,event.x,event.y,
fill="red",width=3,tags='li')
lasty = event.y
lastx = event.x
plant()
執行檔下載