尝试使用python turtle模块进行绘制

尝试使用python turtle模块进行绘制,python,paint,python-turtle,Python,Paint,Python Turtle,我是一个初学者,我正在尝试用python turtle绘制,但我的代码给出了一个错误。我已经尝试了我能想到的一切,但仍然不起作用 from turtle import * from menuitem import MenuItem def changePenColor(c): """Changes the system turtle's color to c.""" color(c) def createMenu(c

我是一个初学者,我正在尝试用python turtle绘制,但我的代码给出了一个错误。我已经尝试了我能想到的一切,但仍然不起作用

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()
如果有人知道我能做些什么来解决这个问题,我很乐意知道。
感谢在
菜单项
类的
初始化
函数的第一行中,使用此

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.\uuuu init\uuuuu(…)
您正在创建该对象的一个实例,并且对其不做任何操作。通过说
super()。\uuuu init\uuuu(…)
,您正在初始化对象的超类,这是您在子类化对象(在本例中为
Turtle
)时始终需要做的事情


注意:您的
\uuuuu init\uuuuu
函数需要缩进,但我假设这是一个粘贴错误。

菜单项
类上的
\uuuuuu init\uuuu
函数的第一行中,使用此选项

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.\uuuu init\uuuuu(…)
您正在创建该对象的一个实例,并且对其不做任何操作。通过说
super()。\uuuu init\uuuu(…)
,您正在初始化对象的超类,这是您在子类化对象(在本例中为
Turtle
)时始终需要做的事情


注意:您的
\uuuuu init\uuuu>函数需要缩进,但我假设这是一个粘贴错误。

您的代码有点混乱。具体而言:

from turtle import *
别这样。特别是在模块中。导入尽可能少的内容以完成工作

createMenu(color)
这应该是
createMenu(changePenColor)
并且
changePenColor()
应该在主模块中定义,而不是在MenuItem类模块中定义

Turtle.__init__(x, y, self, shape = shape, visible = False)
\uuuu init\uuuu
的前三个参数不应该在那里,您应该使用
super
,所有这些都是@Evan所指出的

reset()
self._callBack=callBack
这两种状态实际上都不是ops,可以忽略不计

下面是我对您的代码所做的修改,我相信这些代码会完成您的尝试。例如,我没有使用主模块,而是使用了一个
if uuuuu name_uuuuu=='uuuuuu main_uuu':
进行测试:

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()

您的代码有点混乱。具体而言:

from turtle import *
别这样。特别是在模块中。导入尽可能少的内容以完成工作

createMenu(color)
这应该是
createMenu(changePenColor)
并且
changePenColor()
应该在主模块中定义,而不是在MenuItem类模块中定义

Turtle.__init__(x, y, self, shape = shape, visible = False)
\uuuu init\uuuu
的前三个参数不应该在那里,您应该使用
super
,所有这些都是@Evan所指出的

reset()
self._callBack=callBack
这两种状态实际上都不是ops,可以忽略不计

下面是我对您的代码所做的修改,我相信这些代码会完成您的尝试。例如,我没有使用主模块,而是使用了一个
if uuuuu name_uuuuu=='uuuuuu main_uuu':
进行测试:

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()

你说它不起作用,但不是说它如何起作用。它应该做什么?您期望的输入和输出是什么?你能提供完整的错误信息吗?本质上,我们需要一个完整的解决方案。请阅读并查看您是否可以改进该问题,使我们能够更轻松地帮助您。请将该问题转化为您所说的不起作用的问题,而不是它如何起作用的问题。它应该做什么?您期望的输入和输出是什么?你能提供完整的错误信息吗?本质上,我们需要一个完整的解决方案。请阅读并查看您是否可以改进该问题,使我们能够更轻松地帮助您。请将该问题纳入问题中。您能否解释为什么他们应该使用您的代码?你改变了什么?为什么?我认为这可能是一个很好的答案,再加上一点解释。你能解释一下他们为什么要使用你的代码吗?你改变了什么?为什么?我认为这可能是一个很好的答案,再加上一点解释,你是否也知道如何添加一个选项,让你改变海龟画的线的宽度?谢谢你你也知道如何添加一个选项,让你改变海龟画的线的宽度吗?非常感谢。