[英]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.