首页 > 解决方案 > 如何在python(tkinter)中捕获窗口后面的图像

问题描述

我喜欢捕捉窗口后面的像素,用Pillow包装模糊它们,最后将它们显示为框架上的图像。
除了一个未解决的问题,我什么也找不到:Python Tkinter get image of whats behind a window

是否可以在 Python 3.7 中做到这一点?

以下是代码的示例:

import tkinter as tk
from PIL import Image, ImageTk


class MainWindow(tk.Tk):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.overrideredirect(True)
        self.frame = tk.Frame(self, relief="flat", bd=0)

        self.label = tk.Label()

    def mainloop(self, n=0):
        while self.winfo_exists():
            background = Image.frombytes("RGB", ..., ...)
            tk_backkground = ImageTk.PhotoImage(background)

            self.label.config(image=tk_backkground)
            self.update()
            self.update_idletasks()


if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()

如果您需要更多信息,请直接说出来。

(对不起,我的英语不好)

标签: pythontkinterpython-imaging-libraryblur

解决方案


在 Windows 中,您可以使用 BlurWindow

python -m pip install BlurWindow

from tkinter import *
from ctypes import windll

from BlurWindow.blurWindow import blur

root = Tk()
root.config(bg='green')

root.wm_attributes("-transparent", 'green')
root.geometry('500x400')

root.update()

hWnd = windll.user32.GetForegroundWindow()
blur(hWnd)



def color(hex):
    hWnd = windll.user32.GetForegroundWindow()
    blur(hWnd,hexColor=hex)
    

e = Entry(width=9)
e.insert(0,'#12121240')

e.pack()
b = Button(text='Apply',command=lambda:[color(e.get())])
b.pack()


root.mainloop()

win11


推荐阅读