Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

python - Instance Turtle Object Arguments in vain

Basically, my intention was to create a class to easily create instances of turtle objects rather than copy-pasting code. However, as of now, I have no idea why the arguments are being ignored. I could not find anything to mitigate this. I am very new to coding and sorry if the reason is blindingly obvious I have been trying things out for several hours now to no avail.

This is part of my code that includes my issue:

import turtle

wind = turtle.Screen()
 
wind.title("Turtle") 

wind.bgcolor('#1c2e4a')  
wind.setup(width=800, height=600)  
wind.tracer(0) 


class Audience:

    def __init__(self, setheading, goto):
        self.turtle = turtle.Turtle()
        self.turtle.speed(0)
        self.turtle.shape("turtle")
        self.turtle.color('#bab86c')
        self.turtle.penup()
        self.turtle.goto = goto
        self.turtle.setheading = setheading
        self.turtle.shapesize(stretch_wid=2, stretch_len=2)


audience1 = Audience(180, (0, -260))

while True:

wind.update()
question from:https://stackoverflow.com/questions/65861015/instance-turtle-object-arguments-in-vain

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code appears to be complicated by the fact that Audience contains a turtle instance when it might be simpler for Audience to be a Turtle subclass:

from turtle import Screen, Turtle

class Audience(Turtle):

    def __init__(self, heading, position):
        super().__init__(visible=False)

        self.speed('fastest')
        self.shape('turtle')
        self.color('#bab86c')
        self.penup()
        self.goto(position)
        self.setheading(heading)
        self.shapesize(stretch_wid=2, stretch_len=2)

        self.showturtle()

screen = Screen()
screen.title('Turtle')
screen.bgcolor('#1c2e4a')
screen.setup(width=800, height=600)

audience1 = Audience(180, (0, -260))

screen.mainloop()

Of course, as @Carcigenicate comments (+1) you're setting properties when you should be invoking methods, and you're redefining existing names. And avoid that while True: and update() model, you're heading the wrong direction. Consider ontimer(), and other events, instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...