在 Python 中使用实际绘制的正方形实施形状 Class?

Shape Class implementation in Python with actual drawn square?

我有以下创建形状的代码 class,我有两个问题希望得到解答: 1、当下面代码为运行时,输出为:

>>> 
100
100
None
>>> 

最后的 "None" 是什么,我怎样才能去掉这个输出?

2。理想情况下,我希望能够绘制(在输出屏幕中)一个正方形 。我不想使用 pygame。我确实想知道是否可以整合 turtle 来做到这一点,但不知道如何开始?对使用 turtle 执行此操作的方法有任何建议,或任何其他天才建议?

from turtle import*
class Shape:
    #self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
    #to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
    def __init__(self,x,y):
        self.x=x #the shape has the attribute x (width)
        self.y=y #the shape has the attribute y (height)

    description="The shape has not yet been brought into being"
    author="No one has yet claimed authorship of this shape"

    def area(self):
        return self.x*self.y
    def perimeter(self):
        return 2*self.x+2*self.y
    def describe(self,text):
        self.description =text
    def authorName(self,text):
        self.author=text
    def scaleSize(self,scale):
        self.x=self.x*scale
        self.y=self.y*scale
    def print(self):
        print(self.x)
        print(self.y)


square=Shape(100,100)
print(square.print())

我可能会补充一点,SO 上有一个类似的问题,但没有具体或有用的答案

Using class to draw shapes in turtle

更新:

我试过类似的方法,但无法正常工作。我想我需要在构造函数中的某个地方初始化 turtle - 但在哪里以及如何

from turtle import*
class Shape:
    #self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
    #to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
    def __init__(self,x,y):
        self.x=x #the shape has the attribute x (width)
        self.y=y #the shape has the attribute y (height)


    description="The shape has not yet been brought into being"
    author="No one has yet claimed authorship of this shape"

    def area(self):
        return self.x*self.y
    def perimeter(self):
        return 2*self.x+2*self.y
    def describe(self,text):
        self.description =text
    def authorName(self,text):
        self.author=text
    def scaleSize(self,scale):
        self.x=self.x*scale
        self.y=self.y*scale
    def print(self,shapename):
        print("This shape is a", shapename, "with dimensions:>",self.x,"by",self.y)
    def draw(self):
        turtle.forward(self.x)
        turtle.left(90)
        turtle.forward(se.f.x)
        turtle.left(90)
        turtle.forward(self.y)
        turtle.left(90)
        turtle.forward(self.y)
        turtle.left(90)



square=Shape(100,100)
square.print("square")
print("The perimeter is:",square.perimeter())
print(square.draw())
from turtle import*
class Shape:
    canvas = Screen() # creating object of screen class
    canvas.setup(800,800) # this will setup window on screen with dimension 800x800

    turtle_obj = Turtle() # creating object of turtle class which will be used to plot square on window

    #self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
    #to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
    def __init__(self,x,y):
        self.x=x #the shape has the attribute x (width)
        self.y=y #the shape has the attribute y (height)

    description="The shape has not yet been brought into being"
    author="No one has yet claimed authorship of this shape"

    def area(self):
        return self.x*self.y
    def perimeter(self):
        return 2*self.x+2*self.y
    def describe(self,text):
        self.description =text
    def authorName(self,text):
        self.author=text
    def scaleSize(self,scale):
        self.x=self.x*scale
        self.y=self.y*scale

    def print(self):
        print self.x
        print self.y
        # now we are referring to class object turtle using self.turtle_obj and 
        # calling forward() function which will move plotting cursor in forward direction 
        # upto x pixels and left() will rotate direction by 90 degree 
        self.turtle_obj.forward(self.x)
        self.turtle_obj.left(90)
        self.turtle_obj.forward(self.y)
        self.turtle_obj.left(90)
        self.turtle_obj.forward(self.x)
        self.turtle_obj.left(90)
        self.turtle_obj.forward(self.y)
        self.turtle_obj.left(90)




square=Shape(100,100)
square.print()
  1. print(square.print()) 此行在控制台上生成 None,因为函数未返回任何内容。要删除它只需删除外部 print() 并保持剩余。即 square.print()

  2. 是的,这是可能的,我对你的代码做了一些改动,现在它应该在屏幕上创建正方形。