Python开发

python画图教程,python在线教程

这篇文章主要为大家详细介绍了python实现画图工具,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

–>简易画图工具(Python),供大家参考,具体内容如下

小黑最近在努力的入门python,正好学习到了Python的tkinker模块下的Canvas(画布)和Button(按钮)再加上相应的事务管理,实现了一个简单的画图小工具。

#画图软件的实现from tkinter.filedialog import *from tkinter import *from tkinter.colorchooser import *win_width=900win_height=450class Application(Frame):def __init__(self,master=None,bgcolor=”#000000″):super().__init__(master)self.master=masterself.bgcolor=bgcolorself.x=0self.y=0self.fgcolor=”#ff0000″self.lastDraw=0 #表示最后绘制的图形的idself.startDrawFlag=Falseself.pack()self.createWidget()def createWidget(self):# 创建画板self.drawCad=Canvas(self,width=win_width,height=win_height*0.9,bg=self.bgcolor)self.drawCad.pack()# 创建按钮btn_start = Button(self,text=”开始”,name=”start”)btn_start.pack(side=”left”,padx=10)btn_pen = Button(self, text=”画笔”, name=”pen”)btn_pen.pack(side=”left”, padx=10)btn_rect = Button(self, text=”矩形”, name=”rect”)btn_rect.pack(side=”left”, padx=10)btn_clear = Button(self, text=”清屏”, name=”clear”)btn_clear.pack(side=”left”, padx=10)btn_earsor = Button(self, text=”橡皮擦”, name=”earsor”)btn_earsor.pack(side=”left”, padx=10)btn_line = Button(self, text=”直线”, name=”line”)btn_line.pack(side=”left”, padx=10)btn_lineArrow = Button(self, text=”箭头直线”, name=”lineArrow”)btn_lineArrow.pack(side=”left”, padx=10)btn_color = Button(self, text=”颜色”, name=”color”)btn_color.pack(side=”left”, padx=10)#为按钮绑定事件btn_pen.bind_class(“Button”,”<1>“,self.eventManger)self.drawCad.bind(““,self.stopDraw)def eventManger(self,event):name=event.widget.winfo_name()print(name)if name==”line” :self.drawCad.bind(““,self.myline)elif name==”lineArrow”:self.drawCad.bind(““, self.mylineArrow)elif name==”rect”:self.drawCad.bind(““, self.myRect)elif name==”pen”:self.drawCad.bind(““, self.myPen)elif name==”earsor”:self.drawCad.bind(““, self.myEarsor)elif name==”clear”:self.drawCad.delete(“all”)elif name==”color”:c= askcolor(color=self.fgcolor,title=”画笔选择颜色”)self.fgcolor=c[1]def myline(self,event):self.startDraw(event)self.lastDraw=self.drawCad.create_line(self.x,self.y,event.x,event.y,fill=self.fgcolor)def mylineArrow(self,event):self.startDraw(event)self.lastDraw = self.drawCad.create_line(self.x, self.y, event.x, event.y,arrow=LAST , fill=self.fgcolor)def myRect(self,event):self.startDraw(event)self.lastDraw = self.drawCad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)def myPen(self,event):self.startDraw(event)self.drawCad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)self.x=event.xself.y=event.ydef myEarsor(self,event):self.startDraw(event)self.drawCad.create_rectangle(event.x-4, event.y-4, event.x+4, event.y+4, fill=self.bgcolor)self.x = event.xself.y = event.ydef stopDraw(self,event):self.startDrawFlag=Falseself.lastDraw=0def startDraw(self,event):self.drawCad.delete(self.lastDraw)if not self.startDrawFlag:self.startDrawFlag = Trueself.x = event.xself.y = event.yroot=Tk()app=Application(root)root.title(“简易的画图工具”)root.geometry(str(win_width)+”x”+str(win_height)+”+100+100″)root.mainloop()结果展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注