尝试使用 python 乌龟模块进行绘画

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 class 上 __init__ 函数的第一行,使用这个

super().__init__(shape=shape, visible=False)

而不是

Turtle.__init__(x, y, self, shape = shape, visible = False)

您不需要传入 xyself,因为您已经通过说 self.goto(x, y) 设置了位置。此外,使用 super() 而不是 Turtle,因为您需要初始化 superclass,而不仅仅是 Turtle 的另一个实例。通过说 Turtle.__init__(...) 您正在创建该对象的一个​​实例并且不对其进行任何操作。通过说 super().__init__(...),你正在初始化你的对象的 superclass,这是你在 subclassing 一个对象时总是需要做的(在这种情况下 Turtle).

注意:您的 __init__ 函数需要缩进,但我认为这是粘贴错误。

您的代码有些混乱。具体来说:

from turtle import *

只是不要。特别是在一个模块中。尽可能少地导入以完成工作。

createMenu(color)

这应该是 createMenu(changePenColor) 并且 changePenColor() 应该定义在 main 模块中,而不是 MenuItem class 模块中。

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