画笔&橡皮擦
1 from tkinter import * 2 3 win_width = 900 4 win_height = 500 5 6 7 class Application(Frame): 8 9 def __init__(self, master=None, bgcolor='#000000'): 10 super().__init__(master) 11 self.master = master 12 self.bgcolor = bgcolor 13 self.pack() 14 self.x = 0 15 self.y = 0 16 self.startDrawFlag = False 17 self.lastDraw = 0 18 self.fgcolor = '#ff0000' 19 self.createWidget() 20 21 def createWidget(self): 22 # 创建绘图区 23 self.drawpad = Canvas(root, width=win_width, height=win_height*0.9, bg=self.bgcolor) 24 self.drawpad.pack() 25 26 btn_start = Button(root, text='开始', name='start') 27 btn_start.pack(side='left', padx='10') 28 btn_pen = Button(root, text='画笔', name='pen') 29 btn_pen.pack(side='left', padx='10') 30 btn_rect = Button(root, text='矩形', name='rect') 31 btn_rect.pack(side='left', padx='10') 32 btn_clear = Button(root, text='清屏', name='clear') 33 btn_clear.pack(side='left', padx='10') 34 btn_earser = Button(root, text='橡皮擦', name='eraser') 35 btn_earser.pack(side='left', padx='10') 36 btn_line = Button(root, text='直线', name='line') 37 btn_line.pack(side='left', padx='10') 38 btn_lineArrow = Button(root, text='箭头', name='lineArrow') 39 btn_lineArrow.pack(side='left', padx='10') 40 btn_color = Button(root, text='颜色', name='color') 41 btn_color.pack(side='left', padx='10') 42 43 btn_pen.bind_class('Button', '<1>', self.eventManager) 44 self.drawpad.bind('<ButtonRelease-1>', self.stopDraw) 45 46 47 def eventManager(self, event): 48 name = event.widget.winfo_name() 49 print(name) 50 if name == 'line': 51 self.drawpad.bind('<B1-Motion>', self.myline) 52 elif name == 'lineArrow': 53 self.drawpad.bind('<B1-Motion>', self.mylineArrow) 54 elif name == 'rect': 55 self.drawpad.bind('<B1-Motion>', self.myRect) 56 elif name == 'pen': 57 self.drawpad.bind('<B1-Motion>', self.myPen) 58 elif name == 'eraser': 59 self.drawpad.bind('<B1-Motion>', self.myEraser) 60 61 62 def stopDraw(self, event): 63 self.startDrawFlag = False 64 self.lastDraw = 0 65 66 def startDraw(self, event): 67 self.drawpad.delete(self.lastDraw) 68 if not self.startDrawFlag: 69 self.startDrawFlag = True 70 self.x = event.x 71 self.y = event.y 72 73 def myline(self, event): 74 self.startDraw(event) 75 self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor) 76 77 def mylineArrow(self, event): 78 self.startDraw(event) 79 self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow = LAST, fill=self.fgcolor) 80 81 def myRect(self, event): 82 self.startDraw(event) 83 self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor) 84 85 def myPen(self, event): 86 self.startDraw(event) 87 self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor) 88 self.x = event.x 89 self.y = event.y 90 91 def myEraser(self, event): 92 self.startDraw(event) 93 self.drawpad.create_rectangle(event.x-5, event.y-5, event.x+5, event.y+5, fill=self.bgcolor) 94 95 96 97 if __name__ == '__main__': 98 root = Tk() 99 root.geometry(str(win_width)+'x'+str(win_height)+'+100+100') 100 root.title('绘图') 101 app = Application(master=root) 102 root.mainloop()