Python 3.11 我下载了该模块,但它不工作 pywin32

问题描述 投票:0回答:1

我尝试下载这个名为 pywin32 的模块,它下载了,但它说没有名为 win32api 的模块,我已经这样做了

C:\Users\Maste>pip install pywin32
Requirement already satisfied: pywin32 in c:\users\maste\appdata\local\programs\python\python311\lib\site-packages (306)

Traceback (most recent call last):
  File "C:\Users\Maste\OneDrive\Masaüstü\thecode.txt", line 5, in <module>
    import win32api
ModuleNotFoundError: No module named 'win32api'
import cv2
import os
from mss import mss
import numpy as np
import win32api
import serial
import time
import keyboard
import winsound

os.system("color 5")
os.system("cls")
print("Getting Device Info          ")
print("")
print("Version : 1.3.1              ")
print("")
print("Made with love By Luna       ")
print("")
print("Choose your settings")
print("")

fov = int(input("FOV: "))
print("")

sct = mss()

arduino = serial.Serial('COM6', 115200)

screenshot = sct.monitors[1]
screenshot['left'] = int((screenshot['width'] / 2) - (fov / 2))
screenshot['top'] = int((screenshot['height'] / 2) - (fov / 2))
screenshot['width'] = fov
screenshot['height'] = fov
center = fov/2

embaixo = np.array([140,111,160])
emcima = np.array([148,154,194])

speed = float(input("SPEED: "))
print("")
print("You are ready to go ")
print("")
print("Luna wish you a great day <3 ")
print("")
current_time = time.strftime("    %H:%M:%S    ")
print("The time is", current_time)
print("")

def mousemove(x):
    if x < 0:
        x = x+256
    pax = [int(x)]
    arduino.write(pax)
    arduino.read(2)  # Read the mouse movement data back from the Arduino
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, pax[0], 0, 0, 0)  # Move the mouse on the computer

toggle = True

while True:
    if keyboard.is_pressed("f9"):
        toggle = not toggle
        frequency = 1500
        duration = 100
        winsound.Beep(frequency, duration)
        print("Toggle is ", toggle)
        time.sleep(1)

    if toggle:
        img = np.array(sct.grab(screenshot))
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, embaixo,emcima)
        kernel = np.ones((3,3), np.uint8)
        dilated = cv2.dilate(mask,kernel,iterations= 5)
        thresh = cv2.threshold(dilated, 60, 255, cv2.THRESH_BINARY)[1]
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        if len(contours) != 0:
            mouse = cv2.moments(thresh)
            pixel = (int(mouse["m10"] / mouse["m00"]), int(mouse["m01"] / mouse["m00"]))
            aimzao = pixel[0] + 2
            diff_x = int(aimzao - center)
            alvo = diff_x * speed
        mousemove(alvo)

    #performance delay
    time.sleep(0.01)
python module
1个回答
0
投票

我注意到您在代码中使用

win32api
来移动鼠标,相反,您可以使用不同的模块,例如
pynput
pyautogui
来解决此代码的问题。

这是一个带有

pyautogui
的示例:

import pyautogui

pyautogui.moveTo(100, 100) #moves the mouse to screen coordinates (100, 100)

这很简单。我确实注意到代码中有一些可以改进的地方,例如,

while True:
循环检查
keyboard.is_pressed()
效率稍低,这是使用
keyboard.hotkey()
的更好方法:

#all code above the statement "toggle = True" goes here

class Toggle:
    def __init__(self, frequency, duration, embaixo, emcima, speed):
        self.toggle = True
        self.frequency = frequency
        self.duration = duration
        self.embaixo = embaixo
        self.emcima = emcima
        self.speed = speed

        keyboard.add_hotkey('f9', self.onPress)
    
    def onPress(self):
        self.toggle = not self.toggle
        frequency = 1500
        duration = 100
        winsound.Beep(frequency, duration)
        print("Toggle is ", self.toggle)
        time.sleep(1)

        if self.toggle:
            img = np.array(sct.grab(screenshot))
            hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, self.embaixo, self.emcima)
            kernel = np.ones((3,3), np.uint8)
            dilated = cv2.dilate(mask,kernel,iterations= 5)
            thresh = cv2.threshold(dilated, 60, 255, cv2.THRESH_BINARY)[1]
            contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
            if len(contours) != 0:
                mouse = cv2.moments(thresh)
                pixel = (int(mouse["m10"] / mouse["m00"]), int(mouse["m01"] / mouse["m00"]))
                aimzao = pixel[0] + 2
                diff_x = int(aimzao - center)
                alvo = diff_x * self.speed
            mousemove(alvo)

        time.sleep(0.01)

Toggle(1500, 100, embaixo, emcima, speed)

此代码有效,因此每次按“f9”时,都会调用绑定到热键的函数,并且变量

self.toggle
被否定。然后,在同一个函数中,我们检查 now
self.toggle
是否为 True,如果是,则执行所需的操作。这段代码可能比使用
while True
循环更有效。

如果您想了解有关

pyautogui
pynput
的更多信息,那么这里是两者的文档链接:

pyautogui

pynput

© www.soinside.com 2019 - 2024. All rights reserved.