我正在尝试使用wxPython创建窗口(或框架),该窗口(或框架)是桌面上其他打开的窗口的确切位置和大小。该代码似乎可以正常工作,但是大小和/或位置略有偏离,我不知道为什么。
这是我的代码,用于检查哪些窗口已打开(使用win32ui)并创建wx.Frame:
class WxFrame(wx.Frame):
def __init__(self, parent, style):
super().__init__(None, title='Viewer', style=style)
panel = WxPanel(self)
class WxPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
# callback for determining open windows, locations, and sizes
def callback(hwnd, win_list):
if win32gui.IsWindowVisible(hwnd):
window_title = win32gui.GetWindowText(hwnd)
rect = win32gui.GetWindowRect(hwnd)
x = rect[0]
y = rect[1]
w = rect[2] - x
h = rect[3] - y
name = window_title
location = (x, y)
size = (w, h)
if window_title:
win_list["window_name"].append(name)
win_list["window_location"].append(location)
win_list["window_size"].append(size)
return True
# create a dictionary with info about each open window
win_list = {
"window_name": [],
"window_location": [],
"window_size": []
}
win32gui.EnumWindows(callback, win_list)
app = wx.App()
# loop through each open window, checking if the name is an open calculator
window
for i in range(len(win_list["window_name"])):
if win_list["window_name"][i] == "Calculator":
position = win_list["window_location"][i]
size = win_list["window_size"][i]
frame = WxFrame(None, style=wx.CLIP_CHILDREN | wx.BORDER_NONE )
frame.SetPosition( position )
frame.SetSize(wx.Size(size[0], size[1]))
frame.SetBackgroundColour('BLUE')
frame.Show()
app.MainLoop()
运行此代码时,将创建框架,该框架与桌面上计算器的尺寸非常接近,但与下图所示的图像非常接近:
如您所见,当我运行程序后在任务栏中单击计算器以将计算器显示在框架顶部时,在计算器边缘附近可见蓝色框架,因此框架的大小不正确因为它与计算器的大小不完全匹配。我究竟做错了什么?我尝试运行不带wx.NO_BORDER样式的程序,但随后出现了完全相同的问题,即框架周围有边框而不是没有边框。
感谢您的帮助