简体   繁体   中英

Win32Api Python .SetCursorPos(x,y) how works?

Hello I have this code

import win32api
from tkinter import *

def Cursor():
    win32api.SetCursorPos(10,10)

root = Tk()
root.geometry("500x500")
root.title("")
root.configure(background="#262626")

PlayButton = Button(root, text="Cursor", command=Cursor ).pack()


root.mainloop()

and he give me this error

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
   return self.func(*args)
File "C:\Users\filippo\Desktop\Win32.py", line 5, in Cursor
   win32api.SetCursorPos(10,10)
TypeError: SetCursorPos() takes exactly 1 argument (2 given)

why? he tells me i have to put 1 argument but it should be 2 for x and y axis

Pass the coordinates as a tuple:

def Cursor():
    win32api.SetCursorPos((10,10))
    import win32api, win32con
    pos_x = 1000
    pos_y = 600
    for x in range(pos_x):
        subt = int(pos_x/50)
        win32api.mouse_event(win32con.MOUSE_MOVED, 2, 0)
        if (x+1) == pos_x - subt:
            break

    for x in range(pos_y):
        subt = int(pos_y/50)
        win32api.mouse_event(win32con.MOUSE_MOVED, 0, 2)
        if (x+1) == pos_y - subt:
            break

here in win32api the positions are different means if your screen dimensions are 1366x768 so here with win32api the dimensions are 3000x1687. I try to put cursor on given position using win32api. I use win32api because we can control cursor on games like Minecraft etc. warning :- This code is not 100% accurate here 1-2px add or subtract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM