我正在使用Python向其他程序发送键盘命令。我有一个可行的解决方案,但我正在寻求改进。在
其他程序是Genesys CCPulse reports,我有四个不同的实例同时运行。每个实例都有自己的主窗口,然后在里面有许多子窗口(最多30个)。在
多亏了这篇文章Python Window Activation,我能够在主窗口之间切换,并在前景中得到我需要的窗口。我目前正在使用键盘命令发送菜单快捷键来更改子窗口的焦点并保存它们。在
我想避免菜单导航,只需激活每个子窗口,然后发送命令保存它们。另一篇文章EnumChildWindows not working in pywin32给了我子窗口的句柄列表。在
理想情况下,如果可能的话,我想继续使用win32gui,因为其他代码正在工作。在
当前代码为
import win32gui
import re
class WindowMgr:
#set the wildcard string you will search for
def find_window_wildcard(self, wildcard):
self._handle = None
win32gui.EnumWindows(self.window_enum_callback, wildcard)
#enumurate through all the windows until you find the one you need
def window_enum_callback(self, hwnd, wildcard):
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
self._handle = hwnd ##pass back the id of the window
#as a separate function, set the window to the foreground
def set_foreground(self):
win32gui.SetForegroundWindow(self._handle)
#extra function to get all child window handles
def get_child_handles(self):
win32gui.EnumChildWindows(self._handle, add_to_list, None)
#final function to send the commands in
def flash_window(self):
for c_hwnd in child_list:
print((self._handle),(c_hwnd),(win32gui.GetWindowText(c_hwnd))) #prove correct child window found
#send command1#
#send command2#
#send command3#
#seprate fundction to collect the list of child handles
def add_to_list(hwnd, param):
if win32gui.GetWindowText(hwnd)[:3] == "IWD":
child_list.append(hwnd)
child_list=[]
w = WindowMgr()
w.find_window_wildcard(".*Sales*")
w.set_foreground()
w.get_child_handles()
w.flash_window()
刚刚找到了答案
是
BringWindowToTop
命令依次激活每个子窗口。然后我可以继续向每个窗口发出我想要的任何命令。在相关问题 更多 >
编程相关推荐