欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现画图工具

程序员文章站 2022-09-05 12:43:16
简易画图工具(python),供大家参考,具体内容如下小黑最近在努力的入门python,正好学习到了python的tkinker模块下的canvas(画布)和button(按钮)再加上相应的事务管理,...

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

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

程序代码

#画图软件的实现
from tkinter.filedialog import *
from tkinter import *
from tkinter.colorchooser import *

win_width=900
win_height=450


class application(frame):
  def __init__(self,master=none,bgcolor="#000000"):

    super().__init__(master)
    self.master=master
    self.bgcolor=bgcolor
    self.x=0
    self.y=0
    self.fgcolor="#ff0000"
    self.lastdraw=0 #表示最后绘制的图形的id
    self.startdrawflag=false
    self.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("<buttonrelease-1>",self.stopdraw)
def eventmanger(self,event):
  name=event.widget.winfo_name()
  print(name)
  if name=="line" :
    self.drawcad.bind("<b1-motion>",self.myline)
  elif name=="linearrow":
    self.drawcad.bind("<b1-motion>", self.mylinearrow)
  elif name=="rect":
    self.drawcad.bind("<b1-motion>", self.myrect)
  elif name=="pen":
    self.drawcad.bind("<b1-motion>", self.mypen)
  elif name=="earsor":
    self.drawcad.bind("<b1-motion>", 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.x
  self.y=event.y
def 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.x
  self.y = event.y
def stopdraw(self,event):
  self.startdrawflag=false
  self.lastdraw=0

def startdraw(self,event):
  self.drawcad.delete(self.lastdraw)
  if not self.startdrawflag:
    self.startdrawflag = true
    self.x = event.x
    self.y = event.y
root=tk()
app=application(root)
root.title("简易的画图工具")
root.geometry(str(win_width)+"x"+str(win_height)+"+100+100")
root.mainloop()

结果展示

python实现画图工具

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

相关标签: python 画图工具