Python -Switch state of turtle pen using single keypress

532 views Asked by At

While writing a simple drawing program, I ran into a problem; I want to use the space key to switch the state of the pen from up to down and vice versa.

Of course this could be achieved with an

if event.char == "a key:"
    pen.penup()
elif event.char == "another key":
    pen.pendown()

However this is messy, and not what I am looking for, I want a single key to switch the pen position-

Pseudo-
if event.char == "space":
    if pen.position == up:
        pen.pendown()
    else:
        pen.penup()

Is there a method for doing this? For example with a tkinter widget you have

widgetName.cget("text")

If the widget 'widgetName' is a widget with a text option, this will return the string used as the text option.

What I need is either a pen.switchstate() or a pen.cget("state")

Thanks to anyone who can offer some help.

Edit: As I fixed the problem ,here is the link to the documentation- https://docs.python.org/3.4/library/turtle.html#turtle.isdown

.isdown() will either return True or False, which can be used to make the decision.

1

There are 1 answers

2
Theo Pearson-Bray On BEST ANSWER

Well, I clearly didn't look hard enough. Right there in the documentation is pen.isdown() Sorry, feel free to delete the question.