Python 通过tkinter窗口单击

Python 通过tkinter窗口单击,python,tkinter,window,transparent,click-through,Python,Tkinter,Window,Transparent,Click Through,该函数是从 不仅是窗口没有点击通过,png也是不透明的。 PNG在这里: 这是窗口的外观: 我错过了什么 from tkinter import* import win32gui from win32gui import GetForegroundWindow, ShowWindow, FindWindow, SetWindowLong, GetWindowLong, SetLayeredWindowAttributes from win32con import SW_MINIMIZE, W

该函数是从

不仅是窗口没有点击通过,png也是不透明的。 PNG在这里:

这是窗口的外观:

我错过了什么

from tkinter import*
import win32gui

from win32gui import GetForegroundWindow, ShowWindow, FindWindow, SetWindowLong, GetWindowLong, SetLayeredWindowAttributes
from win32con import SW_MINIMIZE, WS_EX_LAYERED, WS_EX_TRANSPARENT, GWL_EXSTYLE

def setClickthrough(hwnd):
   try:
       styles = GetWindowLong(hwnd, GWL_EXSTYLE)
       styles |= WS_EX_LAYERED | WS_EX_TRANSPARENT
       SetWindowLong(hwnd, GWL_EXSTYLE, styles)
       SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
   except Exception as e:
       print(e)

root = Tk()
root.geometry("100x100")


root.overrideredirect(1)

root.attributes('-topmost', 1)
pic = PhotoImage(file=r'on2.png')
root.wm_attributes("-transparentcolor", 'white')

boardbutton = Label(root, image=pic, bd=0,
                    bg='white')
boardbutton.pack()
setClickthrough(root.winfo_id())
root.mainloop()

我也有一个函数为我的一个应用程序做这项工作,代码看起来与你的非常相似,只是做了一些小的调整。您可以尝试一下:

def set_点击(hwnd,root):
#获取窗口样式并执行“按位或”操作,以使样式分层且透明,从而实现
#clickthrough属性
l_ex_style=win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE)
l_ex_style |=win32con.WS_ex_TRANSPARENT | win32con.WS_ex_
win32gui.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,l_EXSTYLE)
#将窗口设置为透明并始终显示在顶部
win32gui.SetLayeredWindowAttributes(hwnd,win32api.RGB(0,0,0),190,win32con.LWA#u ALPHA)#透明
win32gui.SetWindowPos(hwnd,win32con.hwnd_最上面,root.winfo_x(),root.winfo_y(),0,0,0)
此外,还可以使用以下功能再次禁用clickthrough

def disable_点击(hwnd,根目录):
#再次调用该函数将窗口的扩展样式设置为零,恢复为标准窗口
win32api.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,0)
#如果“选项”中的“始终在顶部”设置为false,请再次删除“始终在顶部”属性
win32gui.SetWindowPos(hwnd,win32con.hwnd_NOTOPMOST,root.winfo_x(),root.winfo_y(),0,0,0)

您可以通过调用
win32gui.FindWindow(None,root.title())
来获取窗口句柄(
hwnd
)。见下面的代码:

from tkinter import *
from PIL import Image, ImageTk
import win32gui
import win32con

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

# 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)
bg = Canvas(root, width=width, height=height, bg='white')

setClickthrough(bg.winfo_id())

frame = ImageTk.PhotoImage(file="example.png")
bg.create_image(1920/2, 1080/2, image=frame)
bg.pack()
root.mainloop()
您的尝试和工作示例之间的重要区别似乎是使用了画布的
hwnd
,而不是窗口


我不是阿贝尔做你想做的事,但我提供了一些代码输出我这个,希望能满足你的需要。额外的代码只是删除了装饰(<代码> OrdReReErdIn(1)< /代码>),并将窗口调整为IMG大小,也将其放置在屏幕的中间。


@martineau我真的没有在脚本中复制并粘贴链接问题的问答。我认为它们之间的重要区别似乎是,在链接的答案中使用了一个画布,而本文的OP试图使用根窗口来实现这一点。我会在我的答案中加入这个想法。所有这些都是有用的信息,应该在答案中。我想你是对的。谢谢你帮我解决点击问题。当您运行它时,它是否会隔离png图像?对我来说,它仍然会在图像周围创建一个透明的白色屏幕。再说一次,我可能把一些琐碎的事情搞砸了again@TonyQu更新。谢谢!尽管我没有要求禁用点击功能,但它确实对我的项目很有帮助
from tkinter import *
from PIL import Image, ImageTk
import win32gui
import win32con

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

def size_position_for_picture():
    bbox = bg.bbox(img_id)
    w,h = bbox[2]-bbox[0],bbox[3]-bbox[1]
    x,y = sw/2-w/2,sh/2-h/2
    root.geometry('%dx%d+%d+%d' % (w,h, x,y))
    bg.configure(width=w,height=h)
    

root = Tk()

sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()

root.overrideredirect(1)
root.attributes("-alpha", 0.75)
root.attributes('-transparentcolor', 'white', '-topmost', 1)
bg = Canvas(root,bg='white',highlightthickness=0)
root.config(bg='white')

setClickthrough(bg.winfo_id())

frame = ImageTk.PhotoImage(file="example.png")
img_id = bg.create_image(0,0, image=frame,anchor='nw')
bg.pack()

size_position_for_picture()
setClickthrough(bg.winfo_id())


root.mainloop()