Python - 函数参数不起作用

时间:2015-09-14 18:47:07

标签: python function tkinter ctypes

我正在制作这个小程序,用户可以在屏幕的x轴和y轴输入他们想要移动鼠标的位置,以及他们想要点击该像素的时间。

我的问题是当我尝试将变量放入此函数时,参数显然无法转换? SetCurPos()是问题,它将采用SetCurPos(x,y),但我收到错误:


    File "C:\Python27\Scripts\ManipulationTools.py", line 13, in click
        SetCursorPos(x,y)
    ArgumentError: argument 1: : Don't know how to convert parameter 1

我的代码:


    from Tkinter import *
    import time
    import ctypes
    #from MoveCursor import click

    class ManipulationTools():

    ##############FUNCTIONS###################################
        def click(x,y, numclicks):
            SetCursorPos = ctypes.windll.user32.SetCursorPos
            mouse_event = ctypes.windll.user32.mouse_event

            SetCursorPos(x,y)
            E1.DELETE(0, END)
            E2.DELETE(0, END)
            E3.DELETE(0, END)

            for i in xrange(numclicks):
                mouse_event(2,0,0,0,0)
                mouse_event(4,0,0,0,0)



    #############END FUNCTIONS################################   
        root = Tk()

        root.maxsize(width=400, height=400)
        root.minsize(width=400, height=400)

        root.config(bg="black")

        L1 = Label(root,text="Enter the x and y value here:", fg="white", bg="black")
        L1.place(x=20, y=20)
        Lx = Label(root, text="X:",fg="white",bg="black")
        Lx.place(x=170,y=20)
        Ly = Label(root, text="Y:",fg="white",bg="black")
        Ly.place(x=240,y=20)
        Lnum = Label(root, text="Number of Times:",fg="white",bg="black")
        Lnum.place(x=150, y=100)

        E1 = Entry(root, width=5, bg="grey", )
        E1.place(x=190,y=20)
        E2 = Entry(root, width=5, bg="grey",)
        E2.place(x=260,y=20)
        E3 = Entry(root, width=5, bg="grey",)
        E3.place(x=260,y=100)

        a=IntVar(E1.get())
        b=IntVar(E2.get())
        c=IntVar(E3.get())


        con = Button(root, command=click(a,b,c), text="Confirm", bg="white")
        con.place(x=300,y=300)

        root.mainloop()

单击按钮确认输入字段中的数字时

我的跟踪错误:


    Traceback (most recent call last):
      File "C:\Python27\Scripts\ManipulationTools.py", line 6, in 
        class ManipulationTools():
      File "C:\Python27\Scripts\ManipulationTools.py", line 53, in ManipulationTools
        con = Button(root, command=click(a,b,c), text="Confirm", bg="white")
      File "C:\Python27\Scripts\ManipulationTools.py", line 13, in click
        SetCursorPos(x,y)
    ArgumentError: argument 1: : Don't know how to convert parameter 1

1 个答案:

答案 0 :(得分:1)

你所谓的####functions####实际上是方法,因此,他们获得的第一个参数始终是对其包含类的实例的引用,通常将其命名为{{1} }。但是,您可以按照自己的意愿命名该参数,这就是:

self

class ManipulationTools(): def click(x,y, numclicks): 是其他地方所谓的x,而不是你在做类似事情时给出的第一个参数

self

正确的做法是:

tools = ManipulationTools()
tools.click(100,200,1) ## this should actually give you an error -- ManipulationTools.click gets called with 4 arguments (self, 100, 200, 1), but is only defined for 3 (self, y, numclicks)