您是否在討論激活您在wx中創建的窗口或單獨的應用程序(如記事本)?如果是wx,那麼它是微不足道的。您只需使用Raise()將您需要的任何框架帶入焦點。您可能會使用PubSub或PostEvent讓子框知道它需要提升。
如果你在談論記事本,那麼事情會變得更加粘性。下面是一個醜陋的黑客我創建基於一些東西,我從網上和PyWin32郵件列表上的不同位置有:
def windowEnumerationHandler(self, hwnd, resultList):
'''
This is a handler to be passed to win32gui.EnumWindows() to generate
a list of (window handle, window text) tuples.
'''
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def bringToFront(self, windowText):
'''
Method to look for an open window that has a title that
matches the passed in text. If found, it will proceed to
attempt to make that window the Foreground Window.
'''
secondsPassed = 0
while secondsPassed <= 5:
# sleep one second to give the window time to appear
wx.Sleep(1)
print 'bringing to front'
topWindows = []
# pass in an empty list to be filled
# somehow this call returns the list with the same variable name
win32gui.EnumWindows(self.windowEnumerationHandler, topWindows)
print len(topWindows)
# loop through windows and find the one we want
for i in topWindows:
if windowText in i[1]:
print i[1]
win32gui.ShowWindow(i[0],5)
win32gui.SetForegroundWindow(i[0])
# loop for 5-10 seconds, then break or raise
handle = win32gui.GetForegroundWindow()
if windowText in win32gui.GetWindowText(handle):
break
else:
# increment counter and loop again
secondsPassed += 1
然後我用的SendKeys包文本發送到窗口(見http://www.rutherfurd.net/python/sendkeys/)。如果用戶打開其他任何東西,腳本將會中斷或發生奇怪的事情。如果您打開MS Office之類的東西,請使用win32com而不是SendKeys。這更可靠。
https://github.com/boppreh/keyboard – Andrew 2017-08-02 20:57:25