深入了解python基于tkinter写的画图项目
程序员文章站
2022-06-22 15:05:17
目录总结# coding=utf-8from tkinter import *from tkinter.filedialog import *from tkinter.colorchooser imp...
# coding=utf-8 from tkinter import * from tkinter.filedialog import * from tkinter.colorchooser import * win_width=900 #定义全局变量 win_height=450 #定义全局变量 class application(frame): #初始化区 def __init__(self,master=none,bg="#000000"): super().__init__(master) self.master=master self.x=0 self.y=0 self.pack() self.bg=bg self.fgcolor="#ff0000" self.lastdraw=0 self.startdrawflag=false self.createwidget() #创建控件 def createwidget(self): #创建画布 self.drawpad=canvas(root,bg=self.bg,width=win_width,height=win_height) self.drawpad.pack() #创建按钮 btnstart=button(root,text="开始",name='start') btnstart.pack(side="left",padx=10) btnpen=button(root,text="画笔",name='pen') btnpen.pack(side="left",padx=10) btnrect=button(root,text="画矩形",name='rect') btnrect.pack(side="left",padx=10) btnclear=button(root,text="清屏",name='clear') btnclear.pack(side="left",padx=10) btneraser=button(root,text="橡皮擦",name='eraser') btneraser.pack(side="left",padx=10) btnline=button(root,text="直线",name='line') btnline.pack(side="left",padx=10) btnlineraw=button(root,text="直线(箭头)",name='lineraw') btnlineraw.pack(side="left",padx=10) btncolor=button(root,text="颜色",name='color') btncolor.pack(side="left",padx=10) #绑定事件处理 btnpen.bind_class("button","<1>",self.eventmanager) self.drawpad.bind("<buttonrelease-1>",self.stopdraw) #绑定键盘快捷键 root.bind("<keypress-g>",self.kuaijiejian) root.bind("<keypress-b>",self.kuaijiejian) def eventmanager(self,event): name=event.widget.winfo_name() print(name) if name=='line': self.drawpad.bind("<b1-motion>",self.linecreate) elif name=='lineraw': self.drawpad.bind("<b1-motion>",self.linearrow) elif name=="rect": self.drawpad.bind("<b1-motion>",self.rect) elif name=="pen": self.drawpad.bind("<b1-motion>",self.pen) elif name=='eraser': self.drawpad.bind("<b1-motion>",self.eraser) elif name=='clear': self.drawpad.delete("all") elif name=='color': s1=askcolor(title="选择颜色",color=self.fgcolor) self.fgcolor=s1[1] def startdraw(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 linecreate(self,event): self.startdraw(event) self.lastdraw=self.drawpad.create_line(self.x,self.y,event.x,event.y,fill=self.fgcolor) def linearrow(self,event): self.startdraw(event) self.lastdraw=self.drawpad.create_line(self.x,self.y,event.x,event.y,arrow=last,fill=self.fgcolor) def rect(self,event): self.startdraw(event) self.lastdraw=self.drawpad.create_rectangle(self.x,self.y,event.x,event.y,outline=self.fgcolor) def pen(self,event): self.startdraw(event) self.lastdraw=self.drawpad.create_line(self.x,self.y,event.x,event.y,fill=self.fgcolor) self.x=event.x self.y=event.y def eraser(self,event): self.startdraw(event) self.drawpad.create_rectangle(self.x-4,self.y-4,event.x+4,event.y+4,fill=self.bg) self.x=event.x self.y=event.y def kuaijiejian(self,event): #设置按钮快捷键 #运行区 if __name__=='__main__': root=tk() root.geometry(str(win_width)+"x"+str(win_height+100)+"+100+100") root.title("画图软件") app=application(master=root) root.mainloop()
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!
上一篇: 生日蛋糕能放几天?生日蛋糕怎么切?