from tkinter import * from tkinter.messagebox import * class Flower() : # define a new class of objects # define the constructor for making new objects of this class def __init__(self, x, y, colr) : self.size = 50 #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 + 20 def draw(self, canvas) : #define a draw method that draws the object on a canvas rad = self.size/2 smallrad = rad/2 canvas.create_rectangle(self.x-3, self.y, self.x+3, self.y+100, fill="green", outline="green") canvas.create_oval(self.x-rad, self.y-rad, self.x+rad, self.y+rad, fill=self.colour) canvas.create_oval(self.x-smallrad, self.y-smallrad, self.x+smallrad, self.y+smallrad, 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() bill = Flower(100, 100, "green") ben = Flower(200, 100, "red") bill.draw(canvas) ben.draw(canvas) showinfo("", "Ready to move them?") canvas.delete(ALL) bill.grow() ben.move(20, 50) bill.draw(canvas) ben.draw(canvas) showinfo("", "All done?") main()