清屏&颜色框

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