2015-09-14 49 views
0

我正在製作這個小程序,用戶可以在其中輸入想要移動鼠標的屏幕的x和y軸,以及他們想要點擊該像素的次數。Python - 函數參數不起作用

我的問題是當我試圖把變量放到這個函數中,參數顯然無法轉換?該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

你叫什麼####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): 
+0

不能你也可以使用拉姆達 – Hippolippo

+0

不太清楚你指的是什麼。所以,我會說:不。 –