简体   繁体   中英

Python -Switch state of turtle pen using single keypress

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM