Data Processing and Visulisation with Python
Python Exercise 9
Turtle
Turtle_example
import turtle
t = turtle.Turtle()
t.fd(100)
t.lt(90)
t.fd(100)
t.left(90)
t.forward(100)
turtle.done()
About turtle
Case Study
drawGride
my method
import turtle
def drawGrid(rowCount, columnCount, cellWidth, cellHeight):
"""
draw a grid with rowCount*columnCount cells.
* rowCount: total number of rows in the grid
* columnCount: total number of columns in the grid
* cellWidth: number of pixels in width for each cell
* cellHeight: number of pixels in height for each cell
so the total number of pixels in width for the whole grid will be (columnCount*cellwidth+1)
the total number of pixels in height for the whole grid will be (rowCount*cellHeight+1)
"""
## You can try and test different paddings
screenPaddingWidth = 6
screenPaddingHeight = 3
gridWidth = columnCount * cellWidth + 1
gridHeight = rowCount * cellHeight + 1
screenWidth = gridWidth + screenPaddingWidth
screenHeight = gridHeight + screenPaddingHeight
screen = turtle.Screen()
screen.setup(screenWidth, screenHeight)
screen.setworldcoordinates(0, 0, screenWidth-1, screenHeight-1)
## put your own name in screen title
screen.title('Grid created by Li YuXiao')
## if you uncomment the two statements with screen.tracer(), what will happen
# screen.tracer(False)
pen = turtle.Turtle()
pen.speed(10)
pen.hideturtle()
## finish the following code to draw out the grid
# draw grid
#-----------------Code Section--------------------
def element():
turtle.forward(cellWidth)
turtle.right(90)
turtle.forward(cellHeight)
turtle.right(90)
turtle.forward(cellWidth)
turtle.right(90)
turtle.forward(cellHeight)
turtle.right(90)
for i in range(columnCount+1):
for j in range(rowCount+1):
turtle.goto(cellWidth*i,cellHeight*j)
turtle.pendown()
element()
turtle.penup()
#--------------End of Code Section----------------
# screen.tracer(True)
screen.exitonclick()
turtle.done()
## finish the following code for the file to run as a script
# if the file is run as a script,
# it will take 4 arguments from the command line and
# calls drawGrid function with these arguments.
#-----------------Code Section--------------------
if __name__ == '__main__':
import sys
numOfRows = int(sys.argv[1])
numOfColumns = int(sys.argv[2])
widthOfCell = int(sys.argv[3])
heightOfCell = int(sys.argv[4])
drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)
#--------------End of Code Section----------------
drawGrid - Helper
import turtle
def drawGrid(rowCount, columnCount, cellWidth, cellHeight):
"""
draw a grid with rowCount*columnCount cells.
* rowCount: total number of rows in the grid
* columnCount: total number of columns in the grid
* cellWidth: number of pixels in width for each cell
* cellHeight: number of pixels in height for each cell
so the total number of pixels in width for the whole grid will be (columnCount*cellwidth+1)
the total number of pixels in height for the whole grid will be (rowCount*cellHeight+1)
"""
## You can try and test different paddings
screenPaddingWidth = 6
screenPaddingHeight = 3
gridWidth = columnCount * cellWidth + 1
gridHeight = rowCount * cellHeight + 1
screenWidth = gridWidth + screenPaddingWidth
screenHeight = gridHeight + screenPaddingHeight
screen = turtle.Screen()
screen.setup(screenWidth, screenHeight)
screen.setworldcoordinates(0, 0, screenWidth-1, screenHeight-1)
## put your own name in screen title
screen.title('Grid created by YE Huanzhuo')
## if you uncomment the two statements with screen.tracer(), what will happen
# screen.tracer(False)
pen = turtle.Turtle()
pen.speed(10)
pen.hideturtle()
## finish the following code to draw out the grid
# draw grid
#-----------------Code Section--------------------
for i in range(rowCount+1):
pen.up()
pen.goto(0, i*cellHeight)
pen.down()
pen.goto(columnCount*cellWidth, i*cellHeight)
for i in range(columnCount+1):
pen.up()
pen.goto(i*cellWidth, 0)
pen.down()
pen.goto(i*cellWidth, rowCount*cellHeight)
#--------------End of Code Section----------------
# screen.tracer(True)
screen.exitonclick()
turtle.done()
## finish the following code for the file to run as a script
# if the file is run as a script,
# it will take 4 arguments from the command line and
# calls drawGrid function with these arguments.
#-----------------Code Section--------------------
if __name__ == '__main__':
import sys
numOfRows = int(sys.argv[1])
numOfColumns = int(sys.argv[2])
widthOfCell = int(sys.argv[3])
heightOfCell = int(sys.argv[4])
drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)
#--------------End of Code Section----------------
drawGridAsUserInput
from drawGrid import drawGrid
numOfRows = int(input('Please input the number of rows in the grid: '))
numOfColumns = int(input('Please input the number of columns in the grid: '))
widthOfCell = int(input('Please input the width in pixels for each cell: '))
heightOfCell = int(input('Please input the height in pixels for each cell: '))
drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)