所以几周前开始学习python,所以可能是一个新手的问题,但是...当我在Windows 10上运行这段代码时:
# code 1
import pyautogui
janela = pyautogui.getWindowsWithTitle('Google Chrome')
janela[0].activate()它聚焦于我可以最大化、关闭、最小化、etc..with pyautogui的所需window....and
但是如果我尝试分配一个kivy按钮,我会得到一个错误。
# code 2 py file
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import pyautogui
class atest(App):
def build(self):
a = bs()
return a
class bs(BoxLayout):
def roda(self):
janela = pyautogui.getWindowsWithTitle('Google Chrome')
janela[0].activate()
ja = atest()
ja.run()#code 2 kv file
<bs>:
Button:
on_press: root.roda()它会渲染按钮,但当单击时:
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_RECT instance instead of pointer to RECT一开始我认为这是pyautogui的问题,所以我用代码1测试了它,它工作了,然后我认为这是我的应用程序的问题,所以我用代码2测试,因为我用一个简单的按钮得到了问题,也许我没有用正确的方式调用它。
遵循这里的建议:https://github.com/asweigart/pyautogui/issues/353,但我认为这是一个不同的问题。
edit: All methods in pyautogui work except the ones related to pygetwindow (getAllWindows(),getWindowsWithTitle('Untitled'), getActiveWindow(),getActiveWindow().title)
edit2: if I change the function called by the button so it runs code1 it works as expected..
def roda(self):
os.system(r"C:\Users\tetsuo\anaconda3\envs\k37\python.exe " "code1.py ")发布于 2021-02-01 08:51:24
因此,放弃了使用pyautogui聚焦窗口,而使用pywinauto:
from pywinauto.application import Application
def roda(self):
app = Application()
app.connect(title_re=".*Chrome")
app_dialog = app.top_window()
app_dialog.set_focus()发布于 2021-03-16 05:20:02
我也遇到了类似的问题,我相信我找到了解决方案。
看起来pywinauto有类似的问题,因为我在他们的github问题页面上找到了一个解决方案的链接。
在此页面上:https://bugs.python.org/issue22552
对于
脚本和应用程序来说,ctype全局LibraryLoader实例非常方便。实际上,它们缓存的是库( CDLL实例),而库又缓存函数指针。
另一方面,Python包不应该使用这些特定的加载器。正如您所经历的,这样做可能会导致restype、argtype和errcheck的定义冲突。而是创建一个私有加载器,如cdll = LibraryLoader(CDLL)或windll = LibraryLoader(WinDLL)。
在非Windows平台上这样做几乎没有意义。为了获得缓存的好处,你必须使用下标,比如cdll'libc.so.6‘。使用libc = CDLL('libc.so.6')会更简单。
并因此编辑pyautogui源文件_pygetwindow_win.py以包含在顶部
windll = ctypes.LibraryLoader(ctypes.WinDLL)
然后更改ctypes.windll的所有调用...而是引用您刚刚创建的windll变量。我猜这更像是pygetwindow中的bug,而不是pyautogui。
https://stackoverflow.com/questions/65767201
复制相似问题