[英]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.我是初学者,我正在尝试用 python 乌龟作画,但我的代码出现错误。 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在MenuItem
类的__init__
函数的第一行中,使用这个
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)
.您不需要传入x
、 y
或self
,因为您已经通过说self.goto(x, y)
设置位置。 Also, use super()
instead of Turtle
, because you need to initialize the superclass, not just another instance of Turtle
.此外,使用super()
而不是Turtle
,因为您需要初始化超类,而不仅仅是Turtle
另一个实例。 By saying Turtle.__init__(...)
you are creating an instance of that object and doing nothing with it.通过说Turtle.__init__(...)
您正在创建该对象的一个实例,并且什么也不做。 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
).通过说super().__init__(...)
,您正在初始化对象的超类,这是您在子类化对象(在本例中为Turtle
)时始终需要执行的操作。
Note: your __init__
function needs to be indented, but I'll assume that was a pasting error.注意:您的__init__
函数需要缩进,但我假设这是一个粘贴错误。
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.这应该是createMenu(changePenColor)
和changePenColor()
应该在主模块中定义,而不是 MenuItem 类模块。
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. __init__
前三个参数不应该在那里,你应该使用super
,正如@Evan 所指出的那样。
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:例如,我没有使用主模块,而是使用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.