简体   繁体   中英

trying to make paint with python turtle module

I'm a beginner and i'm trying to make paint with python turtle but my code gives an error. I've tried everything I could think of but it still isn't working.

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

And this code in a different file:

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

If anyone knows what I can do to fix this, I'd be happy to know. Thanks 😊

In your first line of the __init__ function on your MenuItem class, use this

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

instead of

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

You don't need to pass in x , y , or self , because you are already setting the position by saying self.goto(x, y) . Also, use super() instead of Turtle , because you need to initialize the superclass, not just another instance of Turtle . By saying Turtle.__init__(...) you are creating an instance of that object and doing nothing with it. By saying super().__init__(...) , you are initializing the superclass of your object, which is what you always need to do when you are subclassing an object (in this case Turtle ).

Note: your __init__ function needs to be indented, but I'll assume that was a pasting error.

Your code is somewhat confused. Specifically:

from turtle import *

Just don't. Particularly in a module. Import as little as you need to get the job done.

createMenu(color)

This should be createMenu(changePenColor) and changePenColor() should be defined in the main module, not the MenuItem class module.

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

first three arguments to __init__ shouldn't be there and you should use super , all as @Evan notes.

reset()
self._callBack=callBack

These two statments are effectively no-ops and can be left out.

Below is my rework of your code that I believe does what you're attempting to do. For example purposes, instead of the main module, I just used a if __name__ == '__main__': for testing:

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

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