What's causing this error "struct.error: required argument is not an integer"?
What's causing this error "struct.error: required argument is not an integer"?
我正在尝试制作一个脚本,在打开它时,它会询问我是否要启动滚动的滚动功能或按下右键并等待 5 秒的压力功能
但是当压脚功能被触发时,它给了我这个错误
struct.error: required argument is not an integer
导致此错误的原因是什么?
import time
from pynput.keyboard import Key,Controller
from pynput.mouse import Button, Controller
time.sleep(1)
n = 0
keyboard = Controller()
mouse = Controller()
def presser():
global n
n = n+1
keyboard = Controller()
keyboard.press(Key.right)
keyboard.release(Key.right)
if n == 1:
print('Key Pressed 1 time!')
else:
print('Key Pressed', n ,'times!')
time.sleep(5)
presser()
def scroller():
mouse.scroll(0,-10)
scroller()
time.sleep(1)
def starter():
x = input(' clicker or scroller ').lower()
if x == 'clicker':
presser()
elif x == 'scroller':
scroller()
starter()
回溯错误:-
File "Funk.py", line 30, in <module>
starter()
File "Funk.py", line 27, in starter
presser()
File "Funk.py", line 12, in presser
keyboard.press(Key.right)
File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_base.py", line 90, in press
self._press(button)
File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_xorg.py", line 95, in _press
Xlib.ext.xtest.fake_input(dm, Xlib.X.ButtonPress, button.value)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/ext/xtest.py", line 100, in fake_input
y = y)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1459, in __init__
self._binary = self._request.to_binary(*args, **keys)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1141, in to_binary
return self.to_binary(*varargs, **keys)
File "<string>", line 2, in to_binary
struct.error: required argument is not an integer
您有两个 import 语句,它们从两个不同的模块导入名为 Controller
的东西。大概这些是具有不同用途的不同 classes。我认为第二个导入替换了第一个,所以每次你指的是 Controller
它都是来自鼠标模块的。它出现在 presser()
您想要使用键盘模块中的那个。
所以您调用鼠标控制器的 press() 函数时带有一个用于键盘控制器函数的参数。我怀疑这是错误的原因。
您需要编写导入以使这两个 class 截然不同。您可以通过简单地导入整个模块并完全限定 class 名称来完成此操作。或者您可以通过在输入中为 classes 添加别名来实现,例如:
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
我正在尝试制作一个脚本,在打开它时,它会询问我是否要启动滚动的滚动功能或按下右键并等待 5 秒的压力功能
但是当压脚功能被触发时,它给了我这个错误
struct.error: required argument is not an integer
导致此错误的原因是什么?
import time
from pynput.keyboard import Key,Controller
from pynput.mouse import Button, Controller
time.sleep(1)
n = 0
keyboard = Controller()
mouse = Controller()
def presser():
global n
n = n+1
keyboard = Controller()
keyboard.press(Key.right)
keyboard.release(Key.right)
if n == 1:
print('Key Pressed 1 time!')
else:
print('Key Pressed', n ,'times!')
time.sleep(5)
presser()
def scroller():
mouse.scroll(0,-10)
scroller()
time.sleep(1)
def starter():
x = input(' clicker or scroller ').lower()
if x == 'clicker':
presser()
elif x == 'scroller':
scroller()
starter()
回溯错误:-
File "Funk.py", line 30, in <module>
starter()
File "Funk.py", line 27, in starter
presser()
File "Funk.py", line 12, in presser
keyboard.press(Key.right)
File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_base.py", line 90, in press
self._press(button)
File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_xorg.py", line 95, in _press
Xlib.ext.xtest.fake_input(dm, Xlib.X.ButtonPress, button.value)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/ext/xtest.py", line 100, in fake_input
y = y)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1459, in __init__
self._binary = self._request.to_binary(*args, **keys)
File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1141, in to_binary
return self.to_binary(*varargs, **keys)
File "<string>", line 2, in to_binary
struct.error: required argument is not an integer
您有两个 import 语句,它们从两个不同的模块导入名为 Controller
的东西。大概这些是具有不同用途的不同 classes。我认为第二个导入替换了第一个,所以每次你指的是 Controller
它都是来自鼠标模块的。它出现在 presser()
您想要使用键盘模块中的那个。
所以您调用鼠标控制器的 press() 函数时带有一个用于键盘控制器函数的参数。我怀疑这是错误的原因。
您需要编写导入以使这两个 class 截然不同。您可以通过简单地导入整个模块并完全限定 class 名称来完成此操作。或者您可以通过在输入中为 classes 添加别名来实现,例如:
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController