询问用户在pythontu中要绘制的形状和数量

2024-05-20 00:55:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个程序,要求用户绘制一个形状,以及在pythonturtle中绘制多少个形状。我不知道如何使对话框,以便用户可以说,添加多少,使其正确运行。任何帮助都会很棒!以下是我目前为止的代码:

import turtle

steps = {"triangle": 3, "square": 4, "pentagon": 5, "hexagon": 6, "octagon": 7}

#this is the dialogue box for what shape to draw and moving it over a bit so the 
#next shape can be seen
def onkey_shape():
    shape = turtle.textinput("Enter a shape", "Enter a shape: triangle, 
square, pentagon, hexagon, octagon")
    if shape.lower() in steps:
        turtle.forward(20)
        set_shape(shape.lower())
    turtle.listen()  

def set_shape(shape):
    global current_shape
    turtle.circle(40, None, steps[shape])
    current_shape = shape





turtle.onkey(onkey_shape, "d")

turtle.listen()

turtle.mainloop()

Tags: the用户def绘制steps形状entertriangle
1条回答
网友
1楼 · 发布于 2024-05-20 00:55:04

正如您使用textinput()获得形状一样,您可以使用numinput()来计算有多少个形状:

count = numinput(title, prompt, default=None, minval=None, maxval=None)

下面是对代码的一个重新编写,例如,它只绘制同心形状,您可以在需要的地方绘制它们:

^{pr2}$

您发现了一个棘手的部分,即通常我们在turtle程序中只调用turtle.listen()一次,但是调用textinput()或{}会将侦听器切换到弹出的对话框中,因此我们需要在对话框完成后再次显式地调用turtle.listen()。在

相关问题 更多 >