Ale*_*ell 5 python mouse controls tkinter alpha-transparency
我目前正在通过发送鼠标和击键命令使用 python 控制游戏。我想要做的是在游戏上方放置一个透明的 Tkinter 窗口,以提供一些信息,例如鼠标位置和像素颜色。
我熟悉更改窗口的 alpha 属性以使其透明,但不知道如何始终将该窗口保持在前面并使鼠标单击穿过它。
我目前控制游戏的方法包括在某些位置截取屏幕截图并分析颜色内容。我还需要某种方法来做到这一点,而不会受到 Tkinter 窗口的干扰。
pyscreenshot用于截图 win32api用于点击
谢谢你,亚历克
您可以使用模块SetWindowLong的功能win32gui。如果您想要透明的点击窗口,您必须GWL_EXSTYLE仅在我们的窗口中应用。因此,您需要窗口的窗口句柄。
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 )
Run Code Online (Sandbox Code Playgroud)
如果您想通过 winapi 使用更改窗口的透明度SetLayeredWindowAttributes。
编辑:覆盖始终在顶部的透明窗口的示例代码,它通过点击。它获取当前桌面图像并创建透明叠加层,以便您可以欣赏桌面背景图像。
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()
Run Code Online (Sandbox Code Playgroud)
您可以使用向上/向下箭头键动态更改透明度。请注意,窗框是使用“wx”创建的,但也应该与 tkinter 一起使用。
请随意使用该代码。
| 归档时间: |
|
| 查看次数: |
4002 次 |
| 最近记录: |