我正在尝试制作一个程序,要求用户绘制一个形状,以及在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()
正如您使用
textinput()
获得形状一样,您可以使用numinput()
来计算有多少个形状:下面是对代码的一个重新编写,例如,它只绘制同心形状,您可以在需要的地方绘制它们:
^{pr2}$您发现了一个棘手的部分,即通常我们在turtle程序中只调用}会将侦听器切换到弹出的对话框中,因此我们需要在对话框完成后再次显式地调用
turtle.listen()
一次,但是调用textinput()
或{turtle.listen()
。在相关问题 更多 >
编程相关推荐