[英]trying to make paint with python turtle module
我是初学者,我正在尝试用 python 乌龟作画,但我的代码出现错误。 我已经尝试了所有我能想到的方法,但仍然无法正常工作。
from turtle import *
from menuitem import MenuItem
def changePenColor(c):
"""Changes the system turtle's color to c."""
color(c)
def createMenu(callBack):
"""Displays 6 menu items to respond to the given callback function."""
x = - (window_width() / 2) + 30
y = 100
colors = ('red', 'green', 'blue', 'yellow', 'black', 'purple')
shape = "circle"
for color in colors:
MenuItem(x, y, shape, color, callBack)
y -= 30
def main():
"""Creates a menu for selecting colors."""
reset()
shape("turtle")
createMenu(color)
return "done!"
if __name__=='__main__':
msg = main()
print(msg)
mainloop()
此代码位于不同的文件中:
from turtle import Turtle
class MenuItem(Turtle):
"""Represents a menu item."""
def __init__(self, x, y, shape, color, callBack):
"""Sets the initial state of a menu item."""
Turtle.__init__(x, y, self, shape = shape, visible = False)
self.speed(0)
self.up()
self.goto(x, y)
self.color(color, color)
self._callBack=callBack
self.onclick(lambda x,y: self._callBack(color))
self.showturtle()
如果有人知道我能做些什么来解决这个问题,我很乐意知道。 谢谢😊
在MenuItem
类的__init__
函数的第一行中,使用这个
super().__init__(shape=shape, visible=False)
代替
Turtle.__init__(x, y, self, shape = shape, visible = False)
您不需要传入x
、 y
或self
,因为您已经通过说self.goto(x, y)
设置位置。 此外,使用super()
而不是Turtle
,因为您需要初始化超类,而不仅仅是Turtle
另一个实例。 通过说Turtle.__init__(...)
您正在创建该对象的一个实例,并且什么也不做。 通过说super().__init__(...)
,您正在初始化对象的超类,这是您在子类化对象(在本例中为Turtle
)时始终需要执行的操作。
注意:您的__init__
函数需要缩进,但我假设这是一个粘贴错误。
您的代码有些混乱。 具体来说:
from turtle import *
只是不要。 特别是在一个模块中。 尽可能少地导入以完成工作。
createMenu(color)
这应该是createMenu(changePenColor)
和changePenColor()
应该在主模块中定义,而不是 MenuItem 类模块。
Turtle.__init__(x, y, self, shape = shape, visible = False)
__init__
前三个参数不应该在那里,你应该使用super
,正如@Evan 所指出的那样。
reset()
self._callBack=callBack
这两个语句实际上是空操作,可以省略。
以下是我对您的代码的返工,我相信它可以完成您正在尝试做的事情。 例如,我没有使用主模块,而是使用if __name__ == '__main__':
进行测试:
from turtle import Screen, Turtle
COLORS = ('red', 'green', 'blue', 'yellow', 'black', 'purple')
CURSOR_SIZE = 20
class MenuItem(Turtle):
''' Represents a menu item. '''
def __init__(self, x, y, shape, color, callBack):
''' Sets the initial state of a menu item '''
super().__init__(shape=shape, visible=False)
self.penup()
self.goto(x, y)
self.color(color)
self.onclick(lambda x, y: callBack(color))
self.showturtle()
def createMenu(callBack):
''' Displays 6 menu items to respond to the given callback function. '''
screen = Screen()
x = CURSOR_SIZE * 1.5 - screen.window_width() / 2
y = 100
for color in COLORS:
MenuItem(x, y, 'circle', color, callBack)
y -= CURSOR_SIZE * 1.5
if __name__ == '__main__':
from turtle import getscreen, getturtle
def changePenColor(c):
''' Changes the turtle's color to c. '''
turtle.color(c)
screen = getscreen() # singular screen instance
turtle = getturtle() # default turtle
turtle.shape('turtle')
# Create a menu for selecting colors.
createMenu(changePenColor)
screen.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.