我每秒都要检查一个活动的窗口标题,如下所示:
import ctypes, time
GetForegroundWindow = ctypes.windll.user32.GetForegroundWindow
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
GetWindowText = ctypes.windll.user32.GetWindowTextW
while True:
time.sleep(1)
act_id = GetForegroundWindow()
length = GetWindowTextLength(act_id)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(act_id, buff, length + 1)
print(buff.value)
可行,但打印某些窗口的标题会导致以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\u017d' in position
0: character maps to <undefined>
我该如何解决这个编码错误?
答案 0 :(得分:1)
tar
这很有效。 现在例如'č'打印为'c',这对我来说已经足够了。