询问用户要绘制什么形状以及在Python龟中要绘制多少个形状 [英] Ask user what shape to draw and how many in Python turtle

查看:135
本文介绍了询问用户要绘制什么形状以及在Python龟中要绘制多少个形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a program that asks the user to draw a shape and how many of that shape to draw in Python turtle. I dont know how to make the dialogue box so the user can say how many to add and make it run correctly. Any help will be awesome! Here is my code so far:

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()获取对多少个形状的计数:

Just as you used textinput() to get your shape, you can use numinput() to get your count of how many shapes:

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

这是对代码的重做,出于示例目的,它仅绘制同心的形状-将它们绘制在所需的位置:

Here's a rework of your code, which for example purposes just draws concentric shapes -- you draw them where you want them:

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("Which shape?", "Enter a shape: triangle, square, pentagon, hexagon or octagon")

    if shape.lower() not in STEPS:
        return

    count = turtle.numinput("How many?", "How many {}s?".format(shape), default=1, minval=1, maxval=10)

    turtle.penup()
    turtle.forward(100)
    turtle.pendown()

    set_shape(shape.lower(), int(count))

    turtle.listen()

def set_shape(shape, count):
    turtle.penup()
    turtle.sety(turtle.ycor() - 50)
    turtle.pendown()

    for radius in range(10, 10 - count, -1):
        turtle.circle(5 * radius, steps=STEPS[shape])
        turtle.penup()
        turtle.sety(turtle.ycor() + 5)
        turtle.pendown()


turtle.onkey(onkey_shape, "d")
turtle.listen()

turtle.mainloop()

您发现的棘手部分是,通常我们只在龟程序中调用一次turtle.listen(),但是调用textinput()numinput()会将侦听器切换到弹出的对话框,因此我们需要显式对话框结束后,再次调用turtle.listen().

The tricky part, that you figured out, is that normally we only call turtle.listen() once in a turtle program but invoking textinput() or numinput() switches the listener to the dialog box that pops up so we need to explicitly call turtle.listen() again after the dialogs finish.

这篇关于询问用户要绘制什么形状以及在Python龟中要绘制多少个形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆