如何在执行python turtle命令时将其输出到终端?

How to output python turtle commands to terminal as they are being executed?

请原谅我这样一个笨蛋。我有一个类分配,我需要在执行代码时将代码打印到终端上。这超出了我目前的范围,我找不到一个简单的答案。任何事都会非常感激。再次感谢,再次抱歉。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from turtle import *
import random
s1=Screen()
s1.colormode(255)
t1=Turtle()
t1.ht()
t1.speed(0)


def jumpto():
    x=random.randint(-300,300)
    y=random.randint(-300,300)
    t1.penup()
    t1.goto(x,y)
    t1.pendown()

def drawshape():

    shape=random.choice(["circle","shape","star"])

    if shape=="circle":
        t1.begin_fill()
        x=random.randint(25,200)
        t1.circle(x)
        t1.end_fill()

    if shape=="shape":
        x=random.randint(25,200)
        y=random.randint(3,8)
        t1.begin_fill()
        for n in range (y):

            t1.fd(x)
            t1.rt(360/y)
        t1.end_fill()

    if shape=="star":
         x=random.randint(25,200)

        t1.begin_fill()
         for n in range (5):

            t1.fd(x)
            t1.rt(720/5)
        t1.end_fill()


def randomcolor():
    r=random.randint(0,255)
    g=random.randint(0,255)
    b=random.randint(0,255)
    t1.color(r,g,b)
    r=random.randint(0,255)
    g=random.randint(0,255)
    b=random.randint(0,255)
    t1.fillcolor(r,g,b)
def pensize():
    x=random.randint(1,20)
    t1.pensize(x)

for n in range (1000):

    randomcolor()
    pensize()
    drawshape()
    jumpto()


您可以在Turtle的所有公共成员函数上附加一个修饰符,如本文所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import inspect
import types

def decorator(func):
    def inner(*args, **kwargs):
        print('function: ' + func.__name__)
        print('args: ' + repr(args))
        print('kwargs: ' + repr(kwargs) + '
')
        func(*args, **kwargs)
    return inner

for name, fn in inspect.getmembers(Turtle):
    if isinstance(fn, types.FunctionType):
        if(name[0] != '_'):
            setattr(Turtle, name, decorator(fn))