使用 TkInter 设置不可交互(点击)覆盖 [英] Tying to set non-interactable (click-through) overlay with TkInter

查看:110
本文介绍了使用 TkInter 设置不可交互(点击)覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've gone through several other posts regarding similar issues and all seem to point to this code. Since, I'm working on creating a desktop overlay, I setup a short program to create a top window and hopefully isolate the issue but, despite my efforts I can't figure out why this seemingly widely accepted solution is failing for me or what I could be missing.

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

I've verified that the handle being retrieved is referencing the correct window, trimmed away unnecessary functions, and explored other variations of setting window styles through personal trial and error and mimicking some examples found through 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 窗口透明且可点击:

The TkInter window was successfully made transparent and clickable-through using the solution provided by acw1668 using:

root.attributes('-transparentcolor', 'white', '-topmost', 1)
root.config(bg='white')
root.bg = Canvas(root, width=width, height=height, bg='white')

在画布中创建图像时仍然存在问题.还需要能够点击其他图片:

Problem persists with creating an image in the canvas. Need to be able to have additional images be clickable through as well:

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(),我能够使以下代码工作:

Turns out the handle was not being captured properly using FindWindow and using alternatives such as root.frame() or root.winfo_id() did not match the handle of the window for some reason. By passing in the winfo_id() of the Canvas, I was able to get the below code to work:

    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)

这篇关于使用 TkInter 设置不可交互(点击)覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆