2. Turtle screen
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
s.setup (width=800, height=600, startx=0, starty=0)
# turtle drawing to go here
turtle.done()
2.1. Screen class
turtle.done()
, has been included in the examples below so the window stays open.import turtle
s = turtle.Screen()
# turtle drawing to go here
turtle.done()
2.2. Screen colours
- turtle.colormode(cmode=None)
- cmode - set to one of the values 1.0 or 255. The default config has turtles starting with a cmode of 1.0If colormode is 1, a colour, as a rgb tuple, is written with values from 0 to 1, such as (0.2, 0.8, 0.55).If colormode is 255, a colour, as a rgb tuple, is written with values from 0 to 255, such as (50, 193, 143).If cmode=None, the cmode value is returned.When
s = turtle.Screen()
,s.colormode(cmode=255)
can be used as below.
- turtle.bgcolor(*args)
- args - a color string or three numbers in the range 0 to 1 or 255 depending on the colormode or a 3-tuple of such numbers.Color stings can be a named color such as “orange” or a hex color such as “#800080”.If colormode is 1, a colour is a rgb tuple such as (0.2, 0.8, 0.55),If colormode is 255, a colour is a rgb tuple such as (50, 193, 143)When
s = turtle.Screen()
,s.bgcolor("white")
can be used as below.
import turtle
s = turtle.Screen()
s.bgcolor("white")
# turtle drawing to go here
turtle.done()
import turtle
s = turtle.Screen()
s.colormode(cmode=255)
s.bgcolor((50, 193, 143))
# turtle drawing to go here
turtle.done()
Tasks
Modify the code above to draw a yellow background.
Draw a cyan background using rgb value of (0, 255, 255).
Modify the code above to draw a yellow background.
import turtle
s = turtle.Screen()
s.bgcolor("yellow")
# turtle drawing to go here
turtle.done()
Draw a cyan background using rgb value of (0, 255, 255).
import turtle
s = turtle.Screen()
turtle.colormode(cmode=255)
s.bgcolor((0, 255, 255))
# turtle drawing to go here
turtle.done()
2.3. Screen title
- turtle.title(titlestring)
- titlestring - a string that is shown in the titlebar of the turtle graphics windowWhen
s = turtle.Screen()
,s.title("Turtle Screen")
can be used as below.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
# turtle drawing to go here
turtle.done()
Tasks
Modify the code above to set the screen title to “Turtle Race”.
Modify the code above to set the screen title to “Turtle Race”.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Race")
# turtle drawing to go here
turtle.done()
2.4. Screen size and position
- turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
- Set the size and position of the main window.width - if an integer, a size in pixels; if a float, a fraction of the screen; default 0.5height - if an integer, the height in pixels; if a float, a fraction of the screen; default 0.75startx - if positive, start position in pixels from the left edge of the screen; if negative from the right edge, if None, center window horizontally; default Nonestarty - if positive, start position in pixels from the top edge of the screen; if negative from the bottom edge, if None, center window vertically; default NoneWhen
s = turtle.Screen()
,s.setup()
can be used as below.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
s.setup(width=800, height=600, startx=None, starty=None)
# turtle drawing to go here
turtle.done()
Tasks
Modify the code above to have a screen of 600 by 400, with the left edge 40 pixels from the screen edge and the top edge at the top of the screen.
Modify the code above to have a screen of 600 by 400, with the left edge 40 pixels from the screen edge and the top edge at the top of the screen.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
s.setup(width=600, height=400, startx=40, starty=0)
# turtle drawing to go here
turtle.done()
2.5. Using screen events
See: https://docs.python.org/3/library/turtle.html#turtle.mainloop
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
s.setup (width=800, height=600, startx=0, starty=0)
# turtle drawing to go here
turtle.done()
2.6. Exit on click
- turtle.exitonclick()
- Also turtle.bye()Shut the turtle graphics window when the mouse is clicked on the Screen.When
s = turtle.Screen()
,s.exitonclick()
can be used as below.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Turtle Screen")
s.setup (width=800, height=600, startx=0, starty=0)
# turtle drawing to go here
s.exitonclick()
# turtle.done()
Tasks
Modify the code to require a mouse click to close the turtle window.
import turtle
s = turtle.Screen()
s.bgcolor("lightblue")
s.title("Turtle Screen")
# turtle drawing to go here
turtle.done()
Modify the code above to require a mouse click to close the turtle window.
import turtle
s = turtle.Screen()
s.bgcolor("lightblue")
s.title("Turtle Screen")
# turtle drawing to go here
s.exitonclick()