G20 wants to write a program to poll several important pages and refresh the screen constantly. When calling the SetForegroundWindow function under the pywin32 module, an error occurs: (0, 'setforegroundwindow ', 'No error message is available') an error is reported. After online query, it is confirmed that it is a small bug in the pywin32 module. Before calling this function, you must send another key to the screen, such as the ALT key.
After the SetForegroundWindow is re-encapsulated, the result is as follows:
# Add this import
Import win32com. client
# Add this to _ ini __
Self. shell = win32com. client. Dispatch ("WScript. Shell ")
# And SetAsForegroundWindow becomes
Def SetAsForegroundWindow (self ):
# Send the ALT key. The ALT key is expressed by %.
Self. shell. SendKeys ('% ')
Win32gui. SetForegroundWindow (self. _ hwnd)
To improve common call scenarios, such as window maximization, window hiding, and window placement at the beginning, a class can be easily implemented as follows:
# Coding: UTF-8
Import re, traceback
Import win32gui, win32con, win32com. client
From time import sleep
Class cWindow:
Def _ init _ (self ):
Self. _ hwnd = None
Self. shell = win32com. client. Dispatch ("WScript. Shell ")
Def BringToTop (self ):
Win32gui. BringWindowToTop (self. _ hwnd)
Def SetAsForegroundWindow (self ):
Self. shell. SendKeys ('% ')
Win32gui. SetForegroundWindow (self. _ hwnd)
Def Maximize (self ):
Win32gui. ShowWindow (self. _ hwnd, win32con. SW_MAXIMIZE)
Def setActWin (self ):
Win32gui. SetActiveWindow (self. _ hwnd)
Def _ window_enum_callback (self, hwnd, wildcard ):
'''Pass to win32gui. EnumWindows () to check all the opened windows '''
If re. match (wildcard, str (win32gui. GetWindowText (hwnd) is not None:
Self. _ hwnd = hwnd
Def find_window_wildcard (self, wildcard ):
Self. _ hwnd = None
Win32gui. EnumWindows (self. _ window_enum_callback, wildcard)
Def kill_task_manager (self ):
Wildcard = 'gestionnaire des t. + ches de windows'
Self. find_window_wildcard (wildcard)
If self. _ hwnd:
Win32gui. PostMessage (self. _ hwnd, win32con. WM_CLOSE, 0, 0)
Sleep (0.5)
Def main ():
Sleep (5)
Try:
Wildcard = ". * Building Operation WorkStation .*"
CW = cWindow ()
CW. kill_task_manager ()
CW. find_window_wildcard (wildcard)
CW. BringToTop ()
CW. Maximize ()
CW. SetAsForegroundWindow ()
Except t:
F = open ("log.txt", "w ")
F. write (traceback. format_exc ())
Print (traceback. format_exc ())
If _ name _ = '_ main __':
Main ()
The above operation is already very good. Some people have proposed a further optimization, indicating that the script cannot work normally under some circumstances and two functions are encapsulated, the encapsulated classes are as follows:
Import win32gui, win32con
Import re, traceback
From time import sleep
Class cWindow:
Def _ init _ (self ):
Self. _ hwnd = None
Def SetAsForegroundWindow (self ):
# First, make sure all (other) always-on-top windows are Den.
Self. hide_always_on_top_windows ()
Win32gui. SetForegroundWindow (self. _ hwnd)
Def Maximize (self ):
Win32gui. ShowWindow (self. _ hwnd, win32con. SW_MAXIMIZE)
Def _ window_enum_callback (self, hwnd, regex ):
'''Pass to win32gui. EnumWindows () to check all open windows '''
If self. _ hwnd is None and re. match (regex, str (win32gui. GetWindowText (hwnd) is not None:
Self. _ hwnd = hwnd
Def find_window_regex (self, regex ):
Self. _ hwnd = None
Win32gui. EnumWindows (self. _ window_enum_callback, regex)
Def hide_always_on_top_windows (self ):
Win32gui. EnumWindows (self. _ window_enum_callback_hide, None)
Def _ window_enum_callback_hide (self, hwnd, unused ):
If hwnd! = Self. _ hwnd: # ignore self
# Is the window visible and marked as an always-on-top (topmost) window?
If win32gui. IsWindowVisible (hwnd) and win32gui. GetWindowLong (hwnd, win32con. GWL_EXSTYLE) & win32con. WS_EX_TOPMOST:
# Ignore windows of class 'Click' (the Start Button overlay) and
# 'Shell _ traywnd' (the Task Bar ).
ClassName = win32gui. GetClassName (hwnd)
If not (className = 'Button 'or className = 'shell _ traywnd '):
# Force-minimize the window.
# Fortunately, this seems to work even with windows that
# Have no Minimize button.
# Note that if we tried to hide the window with SW_HIDE,
# It wowould disappear from the Task Bar as well.
Win32gui. ShowWindow (hwnd, win32con. SW_FORCEMINIMIZE)
Def main ():
Sleep (5)
Try:
Regex = ". * Building Operation WorkStation .*"
CW = cWindow ()
CW. find_window_regex (regex)
CW. Maximize ()
CW. SetAsForegroundWindow ()
Except t:
F = open ("log.txt", "w ")
F. write (traceback. format_exc ())
Print (traceback. format_exc ())
Main ()