Python中使用tkinter創建透明圖層、不規則組件、插入GIF動圖等功能實現


心心念念的幾個功能終於實現了,之前在StackOverflow上找了很久都沒有人能實現,今天構思了一下,

果然是透明圖層、不規則組件、插入GIF動圖這一系列問題就是要把握好刷新的時機才能實現的

先來看一下效果:

 

幾個主要函數:

1,自動刷新函數:

》》》注意:不規則的畫布繪圖,以及動態GIF圖一定要寫在此層函數內,不然無法刷新顯示!!《《《

def refresh():
    canvas.create_rectangle(0, 0, canvas.winfo_width(),
                            canvas.winfo_height(), fill=TRANSCOLOUR,
                            outline=TRANSCOLOUR)
    canvas.create_polygon((80,150),(370,150),(430,365),(25,365)
                              ,fill = '#FF4081', width = 0,tags=('LabelRect'))
    canvas.tag_bind('LabelRect',"<Button-1>",Cavas_Click)
    canvas.tag_bind('LabelRect',"<ButtonRelease-1>",Cavas_Release)
    canvas.tag_bind('LabelRect',"<B1-Motion>",OnMotion)
    canvas.create_image(200,100,image=fi,anchor="nw")
    update(1)
    tk.after(100, refresh)

2,鼠標拖動事件:

def Cavas_Click(event):
    global x, y
    x = event.x
    y = event.y
    print('開始移動')
def Cavas_Release(event):
    x = None
    y = None
def OnMotion(event) :
    global x, y
    deltax = event.x - x
    deltay = event.y - y
    _x = tk.winfo_x() + deltax
    _y = tk.winfo_y() + deltay
    tk.geometry( "+%s+%s" % (_x, _y))

3,動態圖片自動更新幀數函數:

def update(flags):
    global ind
##    print(ind)
    if flags:
        try:
            canvas.delete('imageC')
            if (ind == framenum-1):#
                ind = 0
            frame = frames[ind]    
            image1 = canvas.create_image((80,220),image=frame,anchor='w',tags='imageC')
            ind += 1
        except:
            pass

4,主函數區:

if __name__ == '__main__':
    fip="eng.png"#你的透明背景圖片位置
    TRANSCOLOUR = 'gray'

    tk = Tk()
    fi=PhotoImage(file=fip)
    tk.geometry('500x400+500+150')
    tk.title('透明窗體')
    tk.overrideredirect( True)
##    tk.wm_attributes("-topmost", True)    #窗口置頂
##    tk.wm_attributes("-disabled", True)   #窗口禁動
    tk.wm_attributes('-transparentcolor', TRANSCOLOUR)
    tk['bg'] = TRANSCOLOUR

    canvas = Canvas(tk,highlightthickness=0)
    canvas.pack(fill=BOTH, expand=Y)
    L1 = Frame(canvas)
    B1 = Button(L1,text='點擊登錄')
    B1.place(relx=0,rely=0,relwidth=1,relheight=1)
    W1 = canvas.create_window((100,300),window=L1,anchor='w',width=120,height=30)

    tk.after(0, refresh)    #自動刷新

    framenum = 8 # gif 的幀數需要確定下來
    giffile = 'head3.gif' #找一張白色背景的gif,設置白色為透明
    frames = [PhotoImage(file=giffile,format = 'gif -index %s' % i) for i in range(framenum)]

    tk.mainloop()

5,代碼全部合並起來:

from tkinter import *

x,y = 0,0
ind=1
def refresh():
    canvas.create_rectangle(0, 0, canvas.winfo_width(),
                            canvas.winfo_height(), fill=TRANSCOLOUR,
                            outline=TRANSCOLOUR)
    canvas.create_polygon((80,150),(370,150),(430,365),(25,365)
                              ,fill = '#FF4081', width = 0,tags=('LabelRect'))
    canvas.tag_bind('LabelRect',"<Button-1>",Cavas_Click)
    canvas.tag_bind('LabelRect',"<ButtonRelease-1>",Cavas_Release)
    canvas.tag_bind('LabelRect',"<B1-Motion>",OnMotion)
    canvas.create_image(200,100,image=fi,anchor="nw")
    update(1)
    tk.after(100, refresh)
def Cavas_Click(event):
    global x, y
    x = event.x
    y = event.y
    print('開始移動')
def Cavas_Release(event):
    x = None
    y = None
def OnMotion(event) :
    global x, y
    deltax = event.x - x
    deltay = event.y - y
    _x = tk.winfo_x() + deltax
    _y = tk.winfo_y() + deltay
    tk.geometry( "+%s+%s" % (_x, _y))
def update(flags):
    global ind
##    print(ind)
    if flags:
        try:
            canvas.delete('imageC')
            if (ind == framenum-1):#
                ind = 0
            frame = frames[ind]    
            image1 = canvas.create_image((80,220),image=frame,anchor='w',tags='imageC')
            ind += 1
        except:
            pass
if __name__ == '__main__':
    fip="eng.png"#你的透明背景圖片位置
    TRANSCOLOUR = 'gray'

    tk = Tk()
    fi=PhotoImage(file=fip)
    tk.geometry('500x400+500+150')
    tk.title('透明窗體')
    tk.overrideredirect( True)
##    tk.wm_attributes("-topmost", True)    #窗口置頂
##    tk.wm_attributes("-disabled", True)   #窗口禁動
    tk.wm_attributes('-transparentcolor', TRANSCOLOUR)
    tk['bg'] = TRANSCOLOUR

    canvas = Canvas(tk,highlightthickness=0)
    canvas.pack(fill=BOTH, expand=Y)
    L1 = Frame(canvas)
    B1 = Button(L1,text='點擊登錄')
    B1.place(relx=0,rely=0,relwidth=1,relheight=1)
    W1 = canvas.create_window((100,300),window=L1,anchor='w',width=120,height=30)

    tk.after(0, refresh)    #自動刷新

    framenum = 8 # gif 的幀數需要確定下來
    giffile = 'head3.gif' #找一張白色背景的gif,設置白色為透明
    frames = [PhotoImage(file=giffile,format = 'gif -index %s' % i) for i in range(framenum)]

    tk.mainloop()

大功告成!!如果需要更復雜的功能請自行DIY,原理是一樣的。

如有任何疑問,請來企鵝群1137185711咨詢群主吧!

本貼資源及地址:rumba-lun/tk_layout (github.com)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM