"""一个画图软件项目"""
from tkinter import *
from tkinter.colorchooser import *
from tkinter.filedialog import *
#窗口的宽度和高度
win_width = 900
win_hegint =450
class Application(Frame):
def __init__(self,master=None,bgcolor = "#eeeeee"):
super().__init__(master=None)
self.master=master
self.bgcolor = bgcolor
self.x=0
self.y=0
self.fgcolor="#000000"
self.lastDraw=0 #表示绘制最后图形的id
self.startDrawFlag = False
self.pack()
self.createWiget()
def createWiget(self):
# 创建绘图区
self.drawpad = Canvas(root, width=win_width, height=win_hegint*0.9, bg=self.bgcolor)
self.drawpad.pack()
#创建按钮
##开始按钮
btn_start = Button(root,text="开始", name="start")
btn_start.pack(side="left",padx="10")
##画笔按钮
btn_pen = Button(root, text="画笔", name="pen")
btn_pen.pack(side="left", padx="10")
##矩形按钮
btn_rect = Button(root, text="矩形", name="rect")
btn_rect.pack(side="left", padx="10")
##清屏按钮
btn_clear = Button(root, text="清屏", name="clear")
btn_clear.pack(side="left", padx="10")
##橡皮按钮
btn_eraser = Button(root, text="橡皮", name="eraser")
btn_eraser.pack(side="left", padx="10")
##直线按钮
btn_line = Button(root, text="直线", name="line")
btn_line.pack(side="left", padx="10")
##箭头直线按钮
btn_lineArrow = Button(root, text="箭头直线", name="lineArrow")
btn_lineArrow.pack(side="left", padx="10")
##颜色按钮
btn_color = Button(root, text="颜色", name="color")
btn_color.pack(side="left", padx="10")
#事件处理
btn_pen.bind_class("Button","<1>",self.eventManage)
self.drawpad.bind("<ButtonRelease-1>",self.stopDraw)
#增加颜色切换的快捷键
root.bind("<KeyPress-r>", self.kuaijiejian)
root.bind("<KeyPress-g>", self.kuaijiejian)
root.bind("<KeyPress-y>", self.kuaijiejian)
#点击按钮添加事件
def eventManage(self,event):
name = event.widget.winfo_name()
print(name)
if name == "line":
self.drawpad.bind("<B1-Motion>",self.myline)
elif name == "lineArrow":
self.drawpad.bind("<B1-Motion>", self.mylineArrow)
elif name == "rect":
self.drawpad.bind("<B1-Motion>", self.myRect)
elif name == "pen":
self.drawpad.bind("<B1-Motion>", self.myPen)
elif name =="eraser":
self.drawpad.bind("<B1-Motion>", self.myEraser)
elif name == "clear":
self.drawpad.delete("all")
elif name == "color":
c = askcolor(color=self.fgcolor,title="选择画笔颜色")
self.fgcolor =c[1]
#方法封装两个线
def starDraw(self,event):
self.drawpad.delete(self.lastDraw)
if not self.startDrawFlag:
self.startDrawFlag = True
self.x = event.x
self.y = event.y
#如何停止画线
def stopDraw(self,event):
self.startDrawFlag = False
self.lastDraw = 0
#直线画法
def myline(self,event):
self.starDraw(event)#方法在startDraw里面,直接调用
self.lastDraw=self.drawpad.create_line(self.x,self.y,event.x,event.y,fill=self.fgcolor)
#箭头直线画法
def mylineArrow(self,event):
self.starDraw(event)#方法在startDraw里面,直接调用
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST, fill=self.fgcolor)
#矩形画法
def myRect(self,event):
self.starDraw(event) # 方法在startDraw里面,直接调用
self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y,outline=self.fgcolor)
#画笔做法
def myPen(self,event):
self.starDraw(event) # 方法在startDraw里面,直接调用
self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)#不调用删除
self.x = event.x
self.y = event.y
#橡皮擦
def myEraser(self,event):
self.drawpad.create_rectangle(event.x-4, event.y-4, event.x+4, event.y+4, fill=self.bgcolor)
#快捷键的调用
def kuaijiejian(self,event):
if event.char == "r":
self.fgcolor ="#ff0000"
elif event.char == "g":
self.fgcolor ="#00ff00"
elif event.char == "y":
self.fgcolor="#ffff00"
if __name__ == "__main__":
root = Tk()
root.title("画画板")
root.geometry(str(win_width)+"x"+str(win_hegint)+"+500+100")
app=Application(master=root)
root.mainloop()