Page 211 - Bilgisayar Bilimi | Kur 1
P. 211
canvas.itemconfigure(yellow _ lamp, fill=’black’) # Turn yellow off
canvas.itemconfigure(red _ lamp, fill=’red’) # Turn red on
# Create and iitialize global variables
color = ‘red’ # The light’s current color
root = Tk() # Create the main window
root.title(“Traffic Light”)
frame = Frame(root) # Create a frame to hold the widgets
frame.pack() # Make the frame fill the entire window
# Create a drawing surface within the frame
canvas = Canvas(frame, width=150, height=300)
# Set up the visual aspects of the traffic light
# Traffic light frame
canvas.create _ rectangle(50, 20, 150, 280, fill=’gray’)
# Red lamp
red _ lamp = canvas.create _ oval(70, 40, 130, 100, fill=’red’)
# Yellow lamp
yellow _ lamp = canvas.create _ oval(70, 120, 130, 180, fill=’black’)
# Green lamp
green _ lamp = canvas.create _ oval(70, 200, 130, 260, fill=’black’)
# Create a graphical button and ensure it calls the do _ button _
press
# function when the user presses it
button = Button(frame, text=’Change’, command=do _ button _ press)
# Position button in the containing frame’s first row, first column,
# and position canvas in the first row, second column (zero origin,
# like list indices).
button.grid(row=0, column=0)
canvas.grid(row=0, column=1)
# Start the GUI event loop
root.mainloop()
Change
210