溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python基于tkinter寫的畫圖項目分析

發(fā)布時間:2021-12-10 09:01:14 來源:億速云 閱讀:153 作者:iii 欄目:開發(fā)技術

本篇內(nèi)容介紹了“python基于tkinter寫的畫圖項目分析”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

# coding=utf-8
from tkinter import *
from tkinter.filedialog import *
from tkinter.colorchooser import *
win_width=900   #定義全局變量
win_height=450  #定義全局變量
class AppLication(Frame):
    #初始化區(qū)
    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()
    #創(chuàng)建控件
    def CreateWidget(self):
        #創(chuàng)建畫布
        self.drawpad=Canvas(root,bg=self.bg,width=win_width,height=win_height)
        self.drawpad.pack()
        #創(chuàng)建按鈕
        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):
        #設置按鈕快捷鍵
#運行區(qū)
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寫的畫圖項目分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI