from tkinter import * from tkinter.messagebox import * import time class Eye() : # define a new class of objects # define the constructor for making new objects of this class def __init__(self, x, y, colr) : self.size = 10 #give an initial value to the size field self.colour = colr #store the colr in the colour field self.x = x #store the position in the x and y fields self.y = x def move(self, stepX, stepY) : #define a move method that changes object on a canvas self.x = self.x + stepX self.y = self.y + stepY def grow(self) : #define a grow method that makes the object bigger self.size = self.size + 10 def draw(self, canvas) : #define a draw method that draws the object on a canvas wd = self.size ht = wd/2 rad = self.size/4 smallrad = rad/2 canvas.create_oval(self.x-wd, self.y-ht, self.x+wd, self.y+ht, fill=self.colour) canvas.create_oval(self.x-rad, self.y-rad, self.x+rad, self.y+rad, fill="black") #Elsewhere in program, create two objects and draw them: def main() : window = Tk() canvas = Canvas(window, width=450, height=300, bg = 'white') canvas.pack() left = Eye(10, 10, "green") right = Eye(250, 100, "blue") left.draw(canvas) right.draw(canvas) canvas.update() showinfo("", "Ready to move them?") for i in range(20) : canvas.delete(ALL) left.move(2, 5) right.grow() left.draw(canvas) right.draw(canvas) canvas.update() time.sleep(0.2) showinfo("", "All done?") main()