python基于tkinter写的画图项目分析
短信预约 -IT技能 免费直播动态提醒
本篇内容介绍了“python基于tkinter写的画图项目分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
# coding=utf-8from 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()
“python基于tkinter写的画图项目分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341