Tkinter 透视窗口不受鼠标点击的影响 [英] Tkinter see through window not affected by mouse clicks

查看:50
本文介绍了Tkinter 透视窗口不受鼠标点击的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过发送鼠标和击键命令使用 python 控制游戏.我想要做的是在游戏上方放置一个透明的 Tkinter 窗口,以提供一些信息,例如鼠标位置和像素颜色.

I am currently controlling a game with python by sending mouse and keystroke commands. What I am looking to do is have a transparent Tkinter window lay overtop of the game to provide some information such as mouse location and pixel color.

我熟悉更改窗口的 alpha 属性以使其透明,但不知道如何始终将该窗口保持在前面并让鼠标点击通过它.

I am familiar with changing the window's alpha attribute to make it transparent but have no idea how to always keep that window in front and have mouse clicks pass through it.

我目前控制游戏的方法包括在某些位置截取屏幕截图并分析颜色内容.我还需要一些方法来做到这一点,而不会受到 Tkinter 窗口的干扰.

My current method of controlling the game involves taking screenshots in certain locations and analyzing the color content. I will also need some way to do this without the Tkinter window interfering.

Pyscreenshot 用于截图win32api 用于点击

Pyscreenshot is used for screenshots win32api is used for clicking

谢谢,亚历克

推荐答案

你可以使用win32gui模块的SetWindowLong函数.如果你想要一个透明的点击窗口,你必须在我们的窗口上应用 GWL_EXSTYLE.因此,您需要 Window 的窗口句柄.

you can use the SetWindowLong function of win32gui module. If you want a transparent click through window you have to apply GWL_EXSTYLE's ony our window. Therefore you need the windowhandle of your Window.

hwnd = win32gui.FindWindow(None, "Your window title") # Getting window handle
# hwnd = root.winfo_id() getting hwnd with Tkinter windows
# hwnd = root.GetHandle() getting hwnd with wx windows
lExStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
lExStyle |=  win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE , lExStyle )

如果您想通过 winapi 更改窗口的透明度,请使用 SetLayeredWindowAttributes.

If you want to change the transparency of your window via winapi use SetLayeredWindowAttributes.

覆盖始终在顶部的透明窗口的示例代码,通过点击.它获取当前的桌面图像并创建一个透明的叠加层,这样你就可以享受你的桌面背景图像了.

Examplecode for an overlay always-on-top transparent window, which pass through clicks. It gets the current desktopimage and creates a transparent overlay, so you can enjoy your desktop background image.

from win32api import GetSystemMetrics
import win32con
import win32gui
import wx


def scale_bitmap(bitmap, width, height):
    image = wx.ImageFromBitmap(bitmap)
    image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
    result = wx.BitmapFromImage(image)
    return result

app = wx.App()
trans = 50
# create a window/frame, no parent, -1 is default ID
# change the size of the frame to fit the backgound images
frame1 = wx.Frame(None, -1, "KEA", style=wx.CLIP_CHILDREN | wx.STAY_ON_TOP)
# create the class instance
frame1.ShowFullScreen(True)
image_file = win32gui.SystemParametersInfo(win32con.SPI_GETDESKWALLPAPER,0,0)
bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bmp1 = scale_bitmap(bmp1,GetSystemMetrics(1)*1.5,GetSystemMetrics(1))
bitmap1 = wx.StaticBitmap(frame1, -1, bmp1, (-100, 0))
hwnd = frame1.GetHandle()
 
extendedStyleSettings = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, extendedStyleSettings  | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
 
frame1.SetTransparent(trans)


def onKeyDown(e):
    global trans
    key = e.GetKeyCode()
    if  key==wx.WXK_UP:
        print trans
        trans+=10
        if trans >255:
            trans = 255
    elif key==wx.WXK_DOWN:
        print trans
        trans-=10
        if trans < 0:
            trans = 0
    try:
        win32gui.SetLayeredWindowAttributes(hwnd, 0, trans, win32con.LWA_ALPHA)
    except:
        pass
frame1.Bind(wx.EVT_KEY_DOWN, onKeyDown)

app.MainLoop()

您可以使用向上/向下箭头键动态更改透明度.请注意,窗口框架是使用 'wx' 创建的,但也应与 tkinter 一起使用.

You can dynamically change the transparency with the arrow keys Up/Down. Notice, the windowframe is created with 'wx', but should work with tkinter also.

随意使用代码.

这篇关于Tkinter 透视窗口不受鼠标点击的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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