gpt4 book ai didi

python - 使用 TkInter 设置不可交互(点击)覆盖

转载 作者:行者123 更新时间:2023-12-04 10:14:53 28 4
gpt4 key购买 nike

我浏览过其他几篇关于类似问题的帖子,似乎都指向this。代码。因为,我正在创建一个桌面覆盖,我设置了一个简短的程序来创建一个顶部窗口并希望能隔离问题,但是,尽管我努力了,但我无法弄清楚为什么这个看似被广泛接受的解决方案对我来说失败了或者是什么我可能会失踪。

我已经验证所检索的句柄引用了正确的窗口,修剪掉了不必要的功能,并通过个人反复试验探索了设置窗口样式的其他变体,并模仿了通过 programcreek.com 找到的一些示例。

from tkinter import *
from PIL import Image, ImageTk
from win32gui import GetForegroundWindow, ShowWindow, FindWindow,
SetWindowLong, GetWindowLong
from win32con import SW_MINIMIZE, WS_EX_LAYERED, WS_EX_TRANSPARENT, GWL_EXSTYLE

def setClickthrough(root, window="Applepie"):
hwnd = FindWindow(None, window)
styles = GetWindowLong(hwnd, GWL_EXSTYLE)
styles |= WS_EX_LAYERED | WS_EX_TRANSPARENT
print(SetWindowLong(hwnd, GWL_EXSTYLE, styles))

# Dimensions
width = 1920 #self.winfo_screenwidth()
height = 1080 #self.winfo_screenheight()

root = Tk()
root.geometry('%dx%d' % (width, height))
root.title("Applepie")
root.attributes('-transparentcolor', 'white', '-topmost', 1)
root.config(bg='white')
root.attributes("-alpha", 0.25)
root.wm_attributes("-topmost", 1)
root.bg = Canvas(root, width=width, height=height, bg='white')

setClickthrough(root)

frame = ImageTk.PhotoImage(file="Resources/Test/Test-0000.gif")
root.bg.create_image(1920/2, 1080/2, image=frame)
root.bg.pack()
root.mainloop()

使用 acw1668 提供的解决方案成功地使 TkInter 窗口透明且可点击:
root.attributes('-transparentcolor', 'white', '-topmost', 1)
root.config(bg='white')
root.bg = Canvas(root, width=width, height=height, bg='white')

在 Canvas 中创建图像时问题仍然存在。还需要能够点击其他图像:
frame = ImageTk.PhotoImage(file="Resources/Test/Test-0000.gif")
root.bg.create_image(1920/2, 1080/2, image=frame)

最佳答案

事实证明,使用 FindWindow 并没有正确捕获句柄,并且由于某种原因,使用 root.frame() 或 root.winfo_id() 等替代方法与窗口的句柄不匹配。通过传入 Canvas 的 winfo_id(),我可以让下面的代码工作:

    self.root.config(bg='#000000') 
self.root.wm_attributes("-topmost", 1)
self.root.attributes('-transparentcolor', '#000000', '-topmost', 1)

print("Configuring bg")
self.bg = Canvas(self.root, highlightthickness=0)
self.setClickthrough(self.bg.winfo_id())

来电:
def setClickthrough(self, hwnd):
print("setting window properties")
try:
styles = GetWindowLong(hwnd, GWL_EXSTYLE)
styles = WS_EX_LAYERED | WS_EX_TRANSPARENT
SetWindowLong(hwnd, GWL_EXSTYLE, styles)
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA)
except Exception as e:
print(e)

关于python - 使用 TkInter 设置不可交互(点击)覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com