您好,登錄后才能下訂單哦!
簡(jiǎn)易畫圖工具(Python),供大家參考,具體內(nèi)容如下
小黑最近在努力的入門python,正好學(xué)習(xí)到了Python的tkinker模塊下的Canvas(畫布)和Button(按鈕)再加上相應(yīng)的事務(wù)管理,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的畫圖小工具。
程序代碼
#畫圖軟件的實(shí)現(xiàn) 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): # 創(chuàng)建畫板 self.drawCad=Canvas(self,width=win_width,height=win_height*0.9,bg=self.bgcolor) self.drawCad.pack() # 創(chuàng)建按鈕 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("簡(jiǎn)易的畫圖工具") root.geometry(str(win_width)+"x"+str(win_height)+"+100+100") root.mainloop()
結(jié)果展示
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。