win32api.GetCursorPos()问题

时间:2013-11-16 08:02:51

标签: python winapi

所以我一直在乱搞我的鼠标在屏幕上占据一席之地,当我做def时很容易就完成了,并告诉它的线索(32,21) - 但是,每当我使用变量时替换它,它会抛出一个错误,说“SetCursorPos()只需要1个参数(给定2个)”

我该怎么办?

def mPos(cord):
    win32api.SetCursorPos((x,y))

#A bit of code later

def start():
    global h_x
    global h_y #My reasoning for using globals is for the incoming for loop - I don't
    #know how to stop it from creating new "h_x"'s and ignoring ones I already set.

    h_x, h_y = win32api.GetCursorPos() #This works fine.

    print "Mouse was at " + str(h_x) + ", " + str(h_y) #This also displays normally.

    for i in range(countdown): #Consider countdown to be something like "5".
        mPos(h_x,h_y)

mPos(h_x,h_y)是我当前问题的根源。我希望它将那些已经从GetCursorPos()得到的数字重新用于SetCursorPos(),但我不太明白我是怎么做的,或者错误甚至意味着什么。

我甚至得到了它:是的。

 mPos((h_x,h_y))

并且python发布了一个报告,它甚至无法判断出哪种错误......

2 个答案:

答案 0 :(得分:2)

您的功能定义如下:

def mPos(cord):
    win32api.SetCursorPos(x,y)

它收到一个参数。但你称之为传递两个论点。这会导致您报告的错误。

除此之外,函数本身以及对它的调用都会被破坏,因为x和y未在函数中的任何位置定义。改变这样的功能:

def mPos(x, y):
    win32api.SetCursorPos(x,y)

现在你可以传递两个参数的函数。像这样:

mPos(x, y)

最后,我建议一个比mPos更好的名字,而mPos相当被动。使函数成为一个不完美的指令。我称之为SetMousePos或类似的。

答案 1 :(得分:0)

错误消息很明确。您应该传递表示坐标的序列

import time

import win32api

def mPos(x, y):
    win32api.SetCursorPos((x, y))

# OR
#
#def mPos(coord):
#    win32api.SetCursorPos(coord)

def start():
    h_x, h_y = win32api.GetCursorPos() #This works fine.
    print "Mouse was at " + str(h_x) + ", " + str(h_y) #This also displays normally.
    return h_x, h_y

h_x, h_y = start()
for i in range(5):
    mPos(h_x + i * 10, h_y + i * 10)
    time.sleep(1)