from tkinter import * from random import * #-------------------------------------------------- #Define the class of flowers # a flower object contains # its position (x and y) # its height, and # whether it is blooming or not class Flower : # Constructor: create a new flower at the specified position # (The constructor is always called __init__) def __init__(self, xpos, ypos) : # first parameter is the object being created self.x = xpos self.y = ypos self.height = 20; self.blooming = False # make a flower higher def grow(self, increment) : self.height += increment # make a flower bloom def bloom(self) : self.blooming = True # draw a flower def draw(self) : # first parameter is the object that draw was called on stem = 3 # half the width of the stem top = self.y-self.height canvas.create_rectangle(self.x-stem, top, self.x+stem, self.y, fill="green", outline="green") if self.blooming : self.drawBloom(top) else : self.drawBud(top) def drawBloom(self, top) : radius = 15 # radius and colour of the flower head colour = "red" canvas.create_oval(self.x-radius, top-radius, self.x+radius, top+radius, fill=colour, outline=colour) def drawBud(self, top) : radius = 9 # radius and colour of the flower head colour = "green" canvas.create_oval(self.x-radius, top-radius, self.x+radius, top+radius, fill=colour, outline=colour) # ovals and rectangles are specified by left, top, right, bottom. #-------------------------------------------------- # Garden Class # Contains the list of all the flower objects class Garden : def __init__(self) : self.flowers = [] # plant a new flower at the mouse click def plant(self, event) : # parameter is a description of # the mouse event that invoked this flower = Flower(event.x, event.y) # make the flower object self.flowers.append(flower) # put flower object in list of flowers self.drawGarden() # redraw the garden # make each flower in the garden grow def growAll(self) : for flower in self.flowers : flower.grow(10) self.drawGarden() # make each flower in the garden bloom def bloomAll(self) : for flower in self.flowers : flower.bloom() self.drawGarden() # clear all the flowers from the garden and clear the canvas def clear(self) : self.flowers = [] canvas.delete(ALL) # delete everything currently on the canvas canvas.update() # necessary to make the changes in the canvas visible # Draw each flower in the garden def drawGarden(self) : canvas.delete(ALL) for flower in self.flowers : flower.draw() canvas.update() #------------------------------------------------------- # Function to construct the garden and set up the window and canvas to draw on. # The canvas will be accessed by lots of methods, # Since some of those methods are called from buttons, it # is hard to pass it to all the methods that need to use it. # A clean design (but not necessarily the best) is to make it global def setup() : global canvas garden = Garden(); # make the garden (must be done before it is used below) window = Tk() # make a new window canvas = Canvas(window, width=600, height=450, bg='white') # make a new canvas canvas.pack() # pack it into the window (won't show if you don't pack it) butGrow = Button(window, text="Grow", command=garden.growAll) # make new button butGrow.pack() # pack it into the window butBloom = Button(window, text="Bloom", command=garden.bloomAll) # make another button butBloom.pack() Button(window, text="Clear", command=garden.clear).pack() # make and pack button Button(window, text="Exit", command=exit).pack() # make and pack exit button canvas.bind("", garden.plant) # make canvas respond to left mouse window.mainloop() #Start the program by calling setup setup()