简体   繁体   中英

How to fix 'TypeError: MOVE() takes 0 positional arguments but 1 was given' in Python

Trying to make a simple drawing program based on x an y coordinates an i'm using a function to draw and modify the coordinates in one call without actually giving valuea to the function itself using global variables for what needs modification by the function but it still seees as if i've given it actual direct input.

In a previous version i got away by using a class to memorize the ghanging coordinates and functions of the respective class to do the drawing, but in this case the input method is slightly different since i'm using the scale widget isntead of the buttons and as i mentioned before i did try using global variables in both programs actually and it doesn't work in either of them.

from tkinter import *

win=Tk()
win.title("Etch a Schetch")
win.configure(background="grey")
win.configure(bd=5)

global xa
xa=0
global ya
ya=0

def MOVE():
    tabla.create_line(xa,ya,sx.get(),sy.get())
    xa=sx.get()
    ya=sy.get()


tabla=Canvas(win,width=300,height=300)


sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=MOVE)

sy=Scale(win,from_=0,to=300,length=300,command=MOVE)

ex=Button(win,text="Exit",command=exit)


tabla.grid(row=1,column=1)
sx.grid(row=2,column=1)
sy.grid(row=1,column=2)
ex.grid(row=2,column=2)

win.mainloop()

If it would work it would be kinda like an etch a sketch.

I actually just realized what the problem was, to quote mkiever who commented first but i didn't understand untill i did some individuall testing on the interaction between "Scale" and the command that is calling. To put it short and easier to understand the function that is beeing called by "Scale" as its command automaticly takes the value of the scale as an input to the function, as a rezult i only need one declared variable in the paranthesis when i define the function but no paranthesis or input variable is required to give an input to it from the scale.

EXAMPLE:

from tkinter import *
import time

win=Tk()

def P(a):
    print(a)

sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=P)
sx.pack()

win.mainloop()

Some alteration to the code is still required but it'll be much easier without that pesky error showing up.

Thank you everyone for your advice.

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