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 # 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") # rectangles specified by left, top, right, bottom. if self.blooming : radius = 15 # radius and colour of the flower head colour = "red" else : radius = 9 # radius and colour of the bud colour = "green" canvas.create_oval(self.x-radius, top-radius, self.x+radius, top+radius, fill=colour, outline=colour) # ovals also specified by left, top, right, bottom. # make a flower higher def grow(self, increment) : self.height += increment # make a flower bloom def bloom(self) : self.blooming = True; #-------------------------------------------------- # main program # Constructs the unser interface and contains the list of all the flower objects garden = [] # The list of flower objects in the garden # Function to set up the window and canvas to draw on def setup() : global canvas # the canvas will be accessed by lots of methods, # Since some of those methods are called from buttons, it is not possible to pass it to # all the methods that need to use it. Much cleaner to make it global 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=growAll) # make new button for the window butGrow.pack() # pack it into the window butBloom = Button(window, text="Bloom", command=bloomAll) # make another button for the window butBloom.pack() Button(window, text="Clear", command=clear).pack() # make and pack button in one line canvas.bind("", plant) # make the canvas respond to a left mouse button window.mainloop() # plant a new flower at the mouse click def plant(event) : # parameter is a description of the mouse event that invoked this flower = Flower(event.x, event.y) # make the flower object garden.append(flower) # put the flower object in the garden drawGarden() # redraw the garden # make each flower in the garden grow def growAll() : for flower in garden : flower.grow(10) drawGarden() # make each flower in the garden bloom def bloomAll() : for flower in garden : flower.bloom() drawGarden() # clear all the flowers from the garden and clear the canvas def clear() : global garden garden = [] 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() : canvas.delete(ALL) for flower in garden : flower.draw() canvas.update() #Start the program by calling setup setup()