簡體   English   中英

是什么導致了這個錯誤“struct.error: required argument is not an integer”?

[英]What's causing this error "struct.error: required argument is not an integer"?

我正在嘗試制作一個腳本,在打開它時,它會詢問我是否要啟動滾動條 function 或按下右鍵並等待 5 秒的壓力條 function
但是當壓力機 function 被觸發時,它給了我這個錯誤

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的東西。 大概這些是具有不同用途的不同類。 我認為第二個導入正在替換第一個,所以每次你指的是Controller它都是來自鼠標模塊的。 它出現在您想要使用鍵盤模塊中的presser()中。

因此,您正在調用鼠標控制器的 press() function,並帶有一個用於鍵盤控制器的 function 的參數。我懷疑這是錯誤的原因。

您需要編寫導入來區分這兩個類。 您可以通過簡單地導入整個模塊並完全限定 class 名稱來完成此操作。 或者您可以通過為輸入的類添加別名來實現,例如:

from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM