首页 文章

Python - 函数参数不起作用

提问于
浏览
-1

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

My problem 是当我尝试将变量放入此函数时,参数显然无法转换? 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 回答

  • 1

    您所谓的 ####functions#### 实际上是方法,因此,它们获得的第一个参数始终是对其包含类的实例的引用,通常将其命名为 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)
    

    正确的做法是:

    class ManipulationTools():
        def click(self, x,y, numclicks):
    

相关问题