一、主界面實現
(一)實現最簡單的窗體
1
2
3
4
5
6
7
8
9
10
|
from tkinter import * if __name__ = = '__main__' : tk = tk() tk.geometry( '500x400+500+150' ) tk.title( '有趣的透明窗體-開篇了?。?!' ) canvas = canvas(tk) canvas.pack(fill = both, expand = y) tk.mainloop() |
太簡單了,不詳細說了,相信大家都看得懂。
(二)把灰色設置成透明色
1
2
|
transcolour = 'gray' tk.wm_attributes( '-transparentcolor' , transcolour) |
(三)放置一個矩形框在canvas上
1
|
canvas.create_rectangle( 0 , 0 , canvas.winfo_width(), canvas.winfo_height(), fill = transcolour, outline = transcolour) |
(四)讓透明窗體不斷重畫的onsize函數
1
2
3
4
|
def on_resize(evt): tk.configure(width = evt.width,height = evt.height) canvas.create_rectangle( 0 , 0 , canvas.winfo_width(), canvas.winfo_height(), fill = transcolour, outline = transcolour) print (canvas.winfo_width()) |
(五)綁定onsize函數
1
|
tk.bind( '<configure>' , on_resize) |
(六)透明窗體的效果
哈哈,透明主界面效果出來了。
二、gif錄屏功能實現
(一)按次保存成圖片
1
2
3
4
5
6
7
8
9
10
|
def _gifscreen(): global i i + = 1 hwnd = win32gui.findwindow(none, '有趣的透明窗體-開篇了?。?!' ) print (hwnd) rect = win32gui.getwindowrect(hwnd) #獲取當前窗口坐標 rect = (rect[ 0 ] + 10 ,rect[ 1 ] + 32 ,rect[ 0 ] + 10 + canvas.winfo_width(),rect[ 1 ] + 10 + canvas.winfo_height() + 16 ) print (rect) im = imagegrab.grab(rect) #截取目標圖像 im.save( "./out/capturescreen_%s.jpeg" % i, 'jpeg' ) #前面一個參數是保存路徑,后面一個參數是保存格式 |
(二)啟用一個線程
啟用一個線程,調用makegif函數,實現gif圖片的截取
1
2
3
4
|
def gifscreen(): thread_list = [] t1 = threading.thread(target = makegif) thread_list.append(t1) |
(三)實現具體的makegif函數
當生成的圖片達到20張的時候,就開始調用create_gif函數打包成gif文件。
1
2
3
4
5
6
7
8
|
def makegif(): global i,image_list while true: _gifscreen() time.sleep( 0.5 ) if i > 20 : break ; create_gif(image_list, 'out.gif' , 0.5 ) |
(四)實現create_gif,將文件夾里面的圖片打包成gif文件
1
2
3
4
5
6
|
image_list = [] def create_gif(image_list, gif_name, duration = 1.0 ): frames = [] for image_name in image_list: frames.append(imageio.imread(image_name)) imageio.mimsave(gif_name, frames, 'gif' , duration = duration) |
(五)增加錄屏按鈕,并綁定響應事件gifscreen
1
2
|
b = button(tk, text = 'gif截圖' , command = gifscreen) b.pack() |
(六)實現按鈕的響應事件gifscreen
1
2
3
4
5
6
7
8
9
|
def gifscreen(): thread_list = [] t1 = threading.thread(target = makegif) thread_list.append(t1) #正式開啟現線程 for t in thread_list: t.setdaemon(true) t.start() |
三、整體實現效果
(一)界面效果
(二)gif錄屏效果
代碼量不大,效果還行。
再深入研究可以有更多更有趣的應用。
以上就是python基于tkinter實現gif錄屏功能的詳細內容,更多關于python gif錄屏的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/dhjabc_1/article/details/116929385