Python Turtle – Graphics Keyboard Commands
Python Turtle module is a graphical tool that can be used to draw simple graphics on the screen using a cursor. Python Turtle was a part of Logo programming language which had similar purpose of letting the users draw graphics on the screen with the help of simple commands. Turtle is a pre-installed module and has inbuilt commands and features that can be used to draw pictures on the screen. This article will be primarily focussed on creating a graphic using keyboard commands along with how the same methodology can be used to add or change color to the graphic.
- Screen() – used to create a canvas for drawing
- Turtle Motion:
- forward(distance) | fd(distance) : move the turtle forward
- backward(distance) | back(distance) | bk(distance) : move the turtle backwards
- right(distance) | rt(distance) : move the turtle right
- left(distance) | lt(distance) : move the turtle left
- circle(radius) : draws a circle with a given radius
- color() : set the colors
- begin_fill() : this method is called before drawing a shape that is to be filled
- end_fill() : Fills the shape drawn after the call to begin_fill().
Given below are two approaches that deal and discuss how to create a graphics keyboard
- Import module and submodules
- Create setup- The setup() method sets up a window of size 500×500.
- Create window- The Screen() method creates a canvas for drawing.
- Instantiate turtle object
- Set turtle speed to 0 which is maximum
- Set visibility- The showturtle() method sets the visibility of the turtle.
- In order to cature the keystrokes we need to define few functions namely up, down, left, right. By default, the turtle points to the right.
- The setheading() method changes the orientation of the turtle to the given angle.
- The forward() method moves the turtle to the specified distance.
- The listen() method sets focus on the turtle screen to capture events.
- The onkey() method invokes the method specific to the captured keystroke. The first argument of onkey() is the function to be called and the second argument is the key.
- The Up,Down,Left and Right are the corresponding arrow keys on the keyboard.
- Add mainloop() command, it prevents the application from terminating before the user actually clicks the exit option.
Method 2: changing color
This is similar to the previous example with the addition of few more keys. Now we have added keys to change the color of the line.
- If the user presses r it turns red,
- If g it turns green and if b it turns blue.
- For resetting the line color to black the user must press z.
Also, the thickness of the line is increased by setting the width o the turtle to 5px using the width() method.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course