1. """一个画图软件项目"""
    2. from tkinter import *
    3. from tkinter.colorchooser import *
    4. from tkinter.filedialog import *
    5. #窗口的宽度和高度
    6. win_width = 900
    7. win_hegint =450
    8. class Application(Frame):
    9. def __init__(self,master=None,bgcolor = "#eeeeee"):
    10. super().__init__(master=None)
    11. self.master=master
    12. self.bgcolor = bgcolor
    13. self.x=0
    14. self.y=0
    15. self.fgcolor="#000000"
    16. self.lastDraw=0 #表示绘制最后图形的id
    17. self.startDrawFlag = False
    18. self.pack()
    19. self.createWiget()
    20. def createWiget(self):
    21. # 创建绘图区
    22. self.drawpad = Canvas(root, width=win_width, height=win_hegint*0.9, bg=self.bgcolor)
    23. self.drawpad.pack()
    24. #创建按钮
    25. ##开始按钮
    26. btn_start = Button(root,text="开始", name="start")
    27. btn_start.pack(side="left",padx="10")
    28. ##画笔按钮
    29. btn_pen = Button(root, text="画笔", name="pen")
    30. btn_pen.pack(side="left", padx="10")
    31. ##矩形按钮
    32. btn_rect = Button(root, text="矩形", name="rect")
    33. btn_rect.pack(side="left", padx="10")
    34. ##清屏按钮
    35. btn_clear = Button(root, text="清屏", name="clear")
    36. btn_clear.pack(side="left", padx="10")
    37. ##橡皮按钮
    38. btn_eraser = Button(root, text="橡皮", name="eraser")
    39. btn_eraser.pack(side="left", padx="10")
    40. ##直线按钮
    41. btn_line = Button(root, text="直线", name="line")
    42. btn_line.pack(side="left", padx="10")
    43. ##箭头直线按钮
    44. btn_lineArrow = Button(root, text="箭头直线", name="lineArrow")
    45. btn_lineArrow.pack(side="left", padx="10")
    46. ##颜色按钮
    47. btn_color = Button(root, text="颜色", name="color")
    48. btn_color.pack(side="left", padx="10")
    49. #事件处理
    50. btn_pen.bind_class("Button","<1>",self.eventManage)
    51. self.drawpad.bind("<ButtonRelease-1>",self.stopDraw)
    52. #增加颜色切换的快捷键
    53. root.bind("<KeyPress-r>", self.kuaijiejian)
    54. root.bind("<KeyPress-g>", self.kuaijiejian)
    55. root.bind("<KeyPress-y>", self.kuaijiejian)
    56. #点击按钮添加事件
    57. def eventManage(self,event):
    58. name = event.widget.winfo_name()
    59. print(name)
    60. if name == "line":
    61. self.drawpad.bind("<B1-Motion>",self.myline)
    62. elif name == "lineArrow":
    63. self.drawpad.bind("<B1-Motion>", self.mylineArrow)
    64. elif name == "rect":
    65. self.drawpad.bind("<B1-Motion>", self.myRect)
    66. elif name == "pen":
    67. self.drawpad.bind("<B1-Motion>", self.myPen)
    68. elif name =="eraser":
    69. self.drawpad.bind("<B1-Motion>", self.myEraser)
    70. elif name == "clear":
    71. self.drawpad.delete("all")
    72. elif name == "color":
    73. c = askcolor(color=self.fgcolor,title="选择画笔颜色")
    74. self.fgcolor =c[1]
    75. #方法封装两个线
    76. def starDraw(self,event):
    77. self.drawpad.delete(self.lastDraw)
    78. if not self.startDrawFlag:
    79. self.startDrawFlag = True
    80. self.x = event.x
    81. self.y = event.y
    82. #如何停止画线
    83. def stopDraw(self,event):
    84. self.startDrawFlag = False
    85. self.lastDraw = 0
    86. #直线画法
    87. def myline(self,event):
    88. self.starDraw(event)#方法在startDraw里面,直接调用
    89. self.lastDraw=self.drawpad.create_line(self.x,self.y,event.x,event.y,fill=self.fgcolor)
    90. #箭头直线画法
    91. def mylineArrow(self,event):
    92. self.starDraw(event)#方法在startDraw里面,直接调用
    93. self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST, fill=self.fgcolor)
    94. #矩形画法
    95. def myRect(self,event):
    96. self.starDraw(event) # 方法在startDraw里面,直接调用
    97. self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y,outline=self.fgcolor)
    98. #画笔做法
    99. def myPen(self,event):
    100. self.starDraw(event) # 方法在startDraw里面,直接调用
    101. self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)#不调用删除
    102. self.x = event.x
    103. self.y = event.y
    104. #橡皮擦
    105. def myEraser(self,event):
    106. self.drawpad.create_rectangle(event.x-4, event.y-4, event.x+4, event.y+4, fill=self.bgcolor)
    107. #快捷键的调用
    108. def kuaijiejian(self,event):
    109. if event.char == "r":
    110. self.fgcolor ="#ff0000"
    111. elif event.char == "g":
    112. self.fgcolor ="#00ff00"
    113. elif event.char == "y":
    114. self.fgcolor="#ffff00"
    115. if __name__ == "__main__":
    116. root = Tk()
    117. root.title("画画板")
    118. root.geometry(str(win_width)+"x"+str(win_hegint)+"+500+100")
    119. app=Application(master=root)
    120. root.mainloop()

    image.png