Future Learn Oop
Future Learn Oop
# This code will NOT work on trinket.io as the tkinter module is not supported
# Raspberry Pi Foundation 2020
# CC-BY-SA 4.0
try:
from tkinter import Tk, Canvas, BOTH
except ImportError:
raise Exception("tkinter did not import successfully - check you are running
Python 3 and that tkinter is available.")
import random
class Paper():
Args:
width (int): The width of the display. Defaults to 600.
height (int): The height of the display. Defaults to 600.
Returns:
Paper: A Paper object
"""
try:
Paper.tk = Tk()
except ValueError:
raise Exception("Error: could not instantiate tkinter object")
def display(self):
"""
Displays the paper
"""
Paper.tk.mainloop()
class Shape():
# Constructor for Shape
def __init__(self, width=50, height=50, x=None, y=None, color="black"):
"""
Creates a generic 'shape' which contains properties common to all
shapes such as height, width, x y coordinates and colour.
Args:
width (int): The width of the shape. Defaults to 50.
height (int): The height of the shape. Defaults to 50.
x (int): The x position of the shape. If None, the x position will be
the middle of the screen. Defaults to None.
y (int): The y position of the shape. If None, the y position will be
the middle of the screen. Defaults to None.
color (string): The color of the shape. Defaults to "black"
"""
if Paper.tk is None:
raise Exception("A Paper object has not been created. There is nothing
to draw on.")
Args:
smallest (int): The smallest the shape can be. Defaults to 20
largest (int): The largest the the shape can be. Defaults to 200.
"""
self.width = random.randint(smallest, largest)
self.height = random.randint(smallest, largest)
Args:
width (int): The width of the shape
"""
self.width = width
def set_height(self,height):
"""
Sets the height of the shape.
Args:
height (int): The height of the shape.
"""
self.height = height
Args:
x (int): The x position for the shape.
"""
self.x = x
Args:
y (int): The y position for the shape.
"""
self.y = y
Args:
color (string): The color of the shape.
"""
self.color = color
def get_color(self):
"""
Returns the colour of the shape
Returns:
color (string): The color of the shape
"""
return self.color
class Oval(Shape):
def draw(self):
"""
Draws an oval on the canvas. The properties of the oval
can be set using the getter and setter methods in Shape
"""
x1, y1, x2, y2 = self._location()
class Triangle(Shape):
Args:
x1 (int): The x position of the coordinate 1. Defaults to 0.
y1 (int): The y position of the coordinate 1. Defaults to 0.
x2 (int): The x position of the coordinate 2. Defaults to 20.
y2 (int): The y position of the coordinate 2. Defaults to 0.
x3 (int): The x position of the coordinate 3. Defaults to 20.
y4 (int): The y position of the coordinate 3. Defaults to 20.
color (string): The color of the shape. Defaults to "black"
"""
# call the Shape constructor
super().__init__(color=color)
# Remove height and width attributes which make no sense for a triangle
# (triangles are drawn via 3 xy coordinates)
del self.height
del self.width
def _location(self):
"""
Internal method used by the class to get the location
of the triangle. This shouldn't be called by users, hence why its
name begins with an underscore.
"""
return [self.x, self.y, self.x2, self.y2, self.x3, self.y3]
def draw(self):
"""
Draws a triangle on the canvas. The properties of the triangle
can be set using the getter and setter methods in Shape
"""
x1, y1, x2, y2, x3, y3 = self._location()
# Draw a triangle
Paper.tk.canvas.create_polygon(x1, y1, x2, y2, x3, y3, fill=self.color)
def randomize(self):
"""
Randomly chooses the location of all 3 triangle points as well
as the colour of the triangle
"""
# Randomly choose all the points of the triangle
self.x = random.randint(0, Paper.tk.paper_width)
self.y = random.randint(0, Paper.tk.paper_height)
self.x2 = random.randint(0, Paper.tk.paper_width)
self.y2 = random.randint(0, Paper.tk.paper_height)
self.x3 = random.randint(0, Paper.tk.paper_width)
self.y3 = random.randint(0, Paper.tk.paper_height)
Args:
width (int): The width of the shape
"""
self.width = width
def set_height(self,height):
"""
Sets the height of the shape.
Args:
height (int): The height of the shape.
"""
self.height = height
Args:
width (int): The width of the shape
"""
raise Exception("Width cannot be defined for Triangle objects")
Args:
height (int): The height of the shape
"""
raise Exception("Height cannot be defined for Triangle objects")
my_drawing = Paper()
# Default oval
oval = Oval()
oval.draw()
my_drawing.display()