TypeError: function() takes 1 positional argument but 2 were given
TypeError: function() takes 1 positional argument but 2 were given
嗨,我是堆栈溢出的新手,关于我如何 post 这个 post.
可能存在一些问题
所以我想让一只乌龟在鼠标点击的地方生成它自己的克隆,但每次我 运行 代码并点击鼠标时它都会抛出错误:
TypeError: spawn() takes 1 positional argument but 2 were given
我尝试再次检查代码但找不到问题,我决定在这里问。
我的代码:
import turtle
import pyautogui
import tkinter as tk
import keyboard
wn = turtle.Screen()
wn.title('Planner')
wn.bgcolor('white')
wn.setup(width=800,height=800)
wn.tracer(0)
#Turtle 5x0.25:
block_5x025 = turtle.Turtle()
block_5x025.speed(0)
block_5x025.shape('square')
block_5x025.color('black')
block_5x025.shapesize(stretch_wid=5, stretch_len=0.25)
block_5x025.penup()
#Turtle 3x0.25
block_3x025 = turtle.Turtle()
block_3x025.speed(0)
block_3x025.shape('square')
block_3x025.color('black')
block_3x025.shapesize(stretch_wid=3, stretch_len=0.25)
block_3x025.penup()
def clear_screen():
wn.clear()
def get_mouse_click_coor(x, y):
turtle.onscreenclick(None)
#block_5x025.goto(x,y)
print(x, y)
def motion(event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
block_5x025.goto(x-400,-y+400)
block_3x025.goto(x-400,-y+400)
canvas = turtle.getcanvas()
canvas.bind('<Motion>', motion)
class Block:
def __init__(self, size, thickness = 0.25, color = "black"):
self.size = size
self.thickmess = thickness
self.color = color
def spawn_block(self, block):
i = 1
for n in range(1):
globals()["wall" + str(i)] = block.clone()
globals()["wall" + str(i)].goto(block.xcor(),block.ycor())
i += 1
class Wall_5x025(Block):
def __init__(self, size = 5, thickness = 0.25, color = "black"):
super().__init__(size, thickness, color)
def spawn(self):
super().spawn_block(block_5x025)
class Wall_3x025(Block):
def __init__(self, size = 3, thickness = 0.25, color = "black"):
super().__init__(size, thickness, color)
def spawn(self):
super().spawn_block(block_3x025)
def turn():
block_5x025.right(90)
block_3x025.right(90)
def switch():
b3025 = 0
b5025 = 1
if keyboard.is_pressed('e'):
b3025, b5025 = b5025, b3025
if b3025 == 0:
block_3x025.hideturtle()
else:
block_3x025.showturtle()
if b5025 == 0:
block_5x025.hideturtle()
else:
block_5x025.showturtle()
while True:
wn.update()
wn.listen()
turtle.onscreenclick(get_mouse_click_coor)
wn.onkeypress(turn, 'r')
wn.onkeypress(clear_screen, 'c')
turtle.onscreenclick(Wall_5x025.spawn)
switch()
另外,如果有些地方写得不正确或无法理解,我很抱歉...
根据文档,https://docs.python.org/3/library/turtle.html#turtle.onscreenclick、turtle.onscreenclick
接受“一个带有两个参数的函数,该函数将使用 canvas 上单击点的坐标调用”作为参数。您传递给 onscreenclick
的 spawn 方法只接受一个参数,self
。 spawn 方法应该取一对坐标。
我认为您的代码存在的问题超出了将您带到 SO 的问题。例如,while True:
在像 turtle 这样的事件驱动环境中并不是你想要的;您的 clear_screen()
将无法正常工作(它会破坏您的 block_5x025
和 block_3x025
块;您不需要 keyboard
模块;您不需要 tracer()
和 update()
;您不需要使两个示例块保持同步;等等
下面是我对您的代码进行的返工和简化,基本上可以满足您的要求:
from turtle import Screen, Turtle
class Block(Turtle):
def __init__(self, size, thickness=0.25, color="black"):
super().__init__(shape='square', visible=False)
self.shapesize(stretch_wid=size, stretch_len=thickness)
self.color(color)
self.penup()
class Wall_3x025(Block):
def __init__(self, size=3, thickness=0.25, color="black"):
super().__init__(size, thickness, color)
class Wall_5x025(Block):
def __init__(self, size=5, thickness=0.25, color="black"):
super().__init__(size, thickness, color)
# Turtle 3 x 0.25
block_3x025 = Wall_3x025()
# Turtle 5 x 0.25:
block_5x025 = Wall_5x025()
active_block = block_3x025
def switch():
global active_block
screen.onkeypress(None, 'e') # disable handlers inside handler
screen.onclick(None)
if active_block == block_3x025:
active_block = block_5x025
else:
active_block = block_3x025
cursor.shape(active_block.shape())
cursor.shapesize(*active_block.shapesize())
cursor.color(*active_block.color())
screen.onclick(spawn) # reenable handlers
screen.onkeypress(switch, 'e')
blocks = []
def spawn(x, y):
screen.onclick(None)
blocks.append(cursor.stamp())
screen.onclick(spawn)
def motion(event):
canvas.unbind('<Motion>')
x, y = event.x, event.y
cursor.goto(x - 400, 400 - y)
canvas.bind('<Motion>', motion)
def turn():
screen.onkeypress(None, 'r')
cursor.right(90)
screen.onkeypress(turn, 'r')
def clear_walls():
screen.onkeypress(None, 'c')
for block in blocks:
cursor.clearstamp(block)
screen.onkeypress(clear_walls, 'c')
screen = Screen()
screen.title('Planner')
screen.bgcolor('white')
screen.setup(width=800, height=800)
cursor = Turtle(shape=block_3x025.shape())
cursor.shapesize(*block_3x025.shapesize())
cursor.color(*block_3x025.color())
cursor.speed('fastest')
cursor.penup()
canvas = screen.getcanvas()
canvas.bind('<Motion>', motion) # this event not exposed by turtle
screen.onkeypress(turn, 'r')
screen.onkeypress(switch, 'e')
screen.onkeypress(clear_walls, 'c')
screen.onclick(spawn)
screen.listen()
screen.mainloop()
它基于制作邮票而不是克隆海龟。
嗨,我是堆栈溢出的新手,关于我如何 post 这个 post.
可能存在一些问题所以我想让一只乌龟在鼠标点击的地方生成它自己的克隆,但每次我 运行 代码并点击鼠标时它都会抛出错误:
TypeError: spawn() takes 1 positional argument but 2 were given
我尝试再次检查代码但找不到问题,我决定在这里问。 我的代码:
import turtle
import pyautogui
import tkinter as tk
import keyboard
wn = turtle.Screen()
wn.title('Planner')
wn.bgcolor('white')
wn.setup(width=800,height=800)
wn.tracer(0)
#Turtle 5x0.25:
block_5x025 = turtle.Turtle()
block_5x025.speed(0)
block_5x025.shape('square')
block_5x025.color('black')
block_5x025.shapesize(stretch_wid=5, stretch_len=0.25)
block_5x025.penup()
#Turtle 3x0.25
block_3x025 = turtle.Turtle()
block_3x025.speed(0)
block_3x025.shape('square')
block_3x025.color('black')
block_3x025.shapesize(stretch_wid=3, stretch_len=0.25)
block_3x025.penup()
def clear_screen():
wn.clear()
def get_mouse_click_coor(x, y):
turtle.onscreenclick(None)
#block_5x025.goto(x,y)
print(x, y)
def motion(event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
block_5x025.goto(x-400,-y+400)
block_3x025.goto(x-400,-y+400)
canvas = turtle.getcanvas()
canvas.bind('<Motion>', motion)
class Block:
def __init__(self, size, thickness = 0.25, color = "black"):
self.size = size
self.thickmess = thickness
self.color = color
def spawn_block(self, block):
i = 1
for n in range(1):
globals()["wall" + str(i)] = block.clone()
globals()["wall" + str(i)].goto(block.xcor(),block.ycor())
i += 1
class Wall_5x025(Block):
def __init__(self, size = 5, thickness = 0.25, color = "black"):
super().__init__(size, thickness, color)
def spawn(self):
super().spawn_block(block_5x025)
class Wall_3x025(Block):
def __init__(self, size = 3, thickness = 0.25, color = "black"):
super().__init__(size, thickness, color)
def spawn(self):
super().spawn_block(block_3x025)
def turn():
block_5x025.right(90)
block_3x025.right(90)
def switch():
b3025 = 0
b5025 = 1
if keyboard.is_pressed('e'):
b3025, b5025 = b5025, b3025
if b3025 == 0:
block_3x025.hideturtle()
else:
block_3x025.showturtle()
if b5025 == 0:
block_5x025.hideturtle()
else:
block_5x025.showturtle()
while True:
wn.update()
wn.listen()
turtle.onscreenclick(get_mouse_click_coor)
wn.onkeypress(turn, 'r')
wn.onkeypress(clear_screen, 'c')
turtle.onscreenclick(Wall_5x025.spawn)
switch()
另外,如果有些地方写得不正确或无法理解,我很抱歉...
根据文档,https://docs.python.org/3/library/turtle.html#turtle.onscreenclick、turtle.onscreenclick
接受“一个带有两个参数的函数,该函数将使用 canvas 上单击点的坐标调用”作为参数。您传递给 onscreenclick
的 spawn 方法只接受一个参数,self
。 spawn 方法应该取一对坐标。
我认为您的代码存在的问题超出了将您带到 SO 的问题。例如,while True:
在像 turtle 这样的事件驱动环境中并不是你想要的;您的 clear_screen()
将无法正常工作(它会破坏您的 block_5x025
和 block_3x025
块;您不需要 keyboard
模块;您不需要 tracer()
和 update()
;您不需要使两个示例块保持同步;等等
下面是我对您的代码进行的返工和简化,基本上可以满足您的要求:
from turtle import Screen, Turtle
class Block(Turtle):
def __init__(self, size, thickness=0.25, color="black"):
super().__init__(shape='square', visible=False)
self.shapesize(stretch_wid=size, stretch_len=thickness)
self.color(color)
self.penup()
class Wall_3x025(Block):
def __init__(self, size=3, thickness=0.25, color="black"):
super().__init__(size, thickness, color)
class Wall_5x025(Block):
def __init__(self, size=5, thickness=0.25, color="black"):
super().__init__(size, thickness, color)
# Turtle 3 x 0.25
block_3x025 = Wall_3x025()
# Turtle 5 x 0.25:
block_5x025 = Wall_5x025()
active_block = block_3x025
def switch():
global active_block
screen.onkeypress(None, 'e') # disable handlers inside handler
screen.onclick(None)
if active_block == block_3x025:
active_block = block_5x025
else:
active_block = block_3x025
cursor.shape(active_block.shape())
cursor.shapesize(*active_block.shapesize())
cursor.color(*active_block.color())
screen.onclick(spawn) # reenable handlers
screen.onkeypress(switch, 'e')
blocks = []
def spawn(x, y):
screen.onclick(None)
blocks.append(cursor.stamp())
screen.onclick(spawn)
def motion(event):
canvas.unbind('<Motion>')
x, y = event.x, event.y
cursor.goto(x - 400, 400 - y)
canvas.bind('<Motion>', motion)
def turn():
screen.onkeypress(None, 'r')
cursor.right(90)
screen.onkeypress(turn, 'r')
def clear_walls():
screen.onkeypress(None, 'c')
for block in blocks:
cursor.clearstamp(block)
screen.onkeypress(clear_walls, 'c')
screen = Screen()
screen.title('Planner')
screen.bgcolor('white')
screen.setup(width=800, height=800)
cursor = Turtle(shape=block_3x025.shape())
cursor.shapesize(*block_3x025.shapesize())
cursor.color(*block_3x025.color())
cursor.speed('fastest')
cursor.penup()
canvas = screen.getcanvas()
canvas.bind('<Motion>', motion) # this event not exposed by turtle
screen.onkeypress(turn, 'r')
screen.onkeypress(switch, 'e')
screen.onkeypress(clear_walls, 'c')
screen.onclick(spawn)
screen.listen()
screen.mainloop()
它基于制作邮票而不是克隆海龟。