所以我遇到了来自pygetwindow的getWindowsWithTitle函数的问题。当我在getWindowsWithTitle调用之前请求输入时,我会得到以下错误:
Traceback (most recent call last):
File "*****\main.py", line 79, in <module>
handle.activate()
File "*****\venv\lib\site-packages\pygetwindow\_pygetwindow_win.py", line 246, in activate
_raiseWithLastError()
File "*****\venv\lib\site-packages\pygetwindow\_pygetwindow_win.py", line 99, in _raiseWithLastError
raise PyGetWindowException('Error code from Windows: %s - %s' % (errorCode, _formatMessage(errorCode)))
pygetwindow.PyGetWindowException: Error code from Windows: 0 - The operation completed successfully.
如果我注释掉我的输入调用,getWindowsWithTitle就会正常工作。下面是到目前为止我的代码
import win32gui
import time
from pynput.keyboard import Key, Controller
import pygetwindow as window
target = input("** Instance Name Is The Title When You Hover Over The Application ** \nSelect Instance Name: ")
handle = window.getWindowsWithTitle('Command')[0]
keyboard = Controller()
handle.activate()
handle.maximize()
time.sleep(2)
keyboard.press('a')
keyboard.release('a')
我试图获得输入来选择选择哪个窗口,但即使将“目标”放入getWindowsWithTitle,也会给出相同的错误。有人知道为什么在输入后我会得到这个错误吗?
发布于 2021-12-20 12:59:16
我简要地看了一下[GitHub]: asweigart/PyGetWindow - PyGetWindow。我强烈建议不要使用它,因为它是有缺陷的(至少目前的版本如此):
在特定情况下,将引发
,,不能把窗户放在前面
既然你在PyWin32上标注了你的问题,你就可以用它了。
code00.py
#!/usr/bin/env python
import sys
import time
import pynput
import win32con as wcon
import win32gui as wgui
# Callback
def enum_windows_proc(wnd, param):
if wgui.IsWindowVisible(wnd):
text = wgui.GetWindowText(wnd)
if param[0] in text.upper():
print(wnd, text) # Debug purposes only
param[1].append(wnd)
def windows_containing_text(text):
ret = []
wgui.EnumWindows(enum_windows_proc, (text.upper(), ret))
return ret
def send_char(wnd, char):
kb = pynput.keyboard.Controller()
kb.press(char)
kb.release(char)
def main(*argv):
txt = input("Enter text contained by window title: ")
#txt = "notepad"
wnds = windows_containing_text(txt)
print(wnds)
wnd = wnds[1] # Notepad in my case (this example only, list might have fewer elements!!!!!)
try:
wgui.SetForegroundWindow(wnd) # Activate
except:
print(sys.exc_info())
wgui.ShowWindow(wnd, wcon.SW_MAXIMIZE) # Maximize
time.sleep(1)
send_char(wnd, "a")
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.")
sys.exit(rc)
有关此[SO]: Get the title of a window of another program using the process name (@CristiFati's answer)区域的更多详细信息,请查看WinAPI。
输出
cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q070373526> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe“编码00.py Python3.8.7(标签/v3.8.7:6503f05,2020年12月21日,17:59:51) MSC v.1928 64位(AMD64) 064位(AMD64)064位(AMD64)064位,输入窗口标题包含的文本: Dec 394988 e:\Work\Dev\StackOverflow\q070373526\code00.py - Notepad++管理员4264044 *无标题记事本394988,4264044完成。
"a" char 插入记事本窗口。。
https://stackoverflow.com/questions/70373526
复制相似问题