我使用pip install pywin32
然后我可以使用以下方法导入包:
from win32api import keybd_event, SetCursorPos, mouse_event
但在我的代码中访问它时:
def press(*args):
'''
one press, one release.
accepts as many arguments as you want. e.g. press('left_arrow', 'a','b').
'''
for i in args:
win32api.keybd_event(VK_CODE[i], 0,0,0)
time.sleep(.05)
win32api.keybd_event(VK_CODE[i],0 ,win32con.KEYEVENTF_KEYUP ,0)
我收到如下所示的错误。有没有人对可能出错的东西有任何见解,或者对我来说如何最好地确定根本原因。
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\Users\bartis\Desktop\Python\Point\sendKeys.py", line 164, in <module>
press('backspace')
File "C:\Users\bartis\Desktop\Python\Point\sendKeys.py", line 121, in press
win32api.keybd_event(VK_CODE[i], 0,0,0)
NameError: name 'win32api' is not defined
答案 0 :(得分:2)
您使用的from
语句仅导入import
关键字后面列出的名称。它不会导入名称win32api
本身。添加import win32api
语句或将win32api.keybd_event
更改为keybd_event
。