onkey ( advance_state_machine, "space" ) wn. fillcolor ( "green" ) state_num = 0 # Bind the event handler to the space key. fillcolor ( "red" ) state_num = 2 else : # Transition from state 2 to state 0 tess. fillcolor ( "orange" ) state_num = 1 elif state_num = 1 : # Transition from state 1 to state 2 tess. # This variable holds the current state of the machine state_num = 0 def advance_state_machine (): global state_num if state_num = 0 : # Transition from state 0 to state 1 tess. We number these states 0, 1, 2 # When the machine changes state, we change tess' position and # her fillcolor.
![python event driven programming python event driven programming](https://cdn.confluent.io/wp-content/uploads/Materialized_Views.png)
fillcolor ( "green" ) # A traffic light is a kind of state machine with three states, # Green, Orange, Red. forward ( 50 ) # Turn tess into a big green circle tess. penup () # Position tess onto the place where the green light should be tess. Turtle () def draw_housing (): """ Draw a nice housing to hold the traffic lights """ tess. title ( "Tess becomes a traffic light!" ) wn. Import turtle # Tess becomes a traffic light. Next (Page Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, Shift_L (any Shift key), Control_L (any Control key), Alt_L (any Alt key), Pause, Caps_Lock, Escape, Prior (Page Up), Some of the symbolic names to try are Cancel (the Break key), BackSpace, Tab, Return(the Enter key),
#PYTHON EVENT DRIVEN PROGRAMMING CODE#
![python event driven programming python event driven programming](https://slidetodoc.com/presentation_image/ca9ddb3480a1e5a5ad2f7a294d2c1af5/image-26.jpg)
bye () # Close down the turtle window # These lines "wire up" keypresses to the handlers we've defined.
![python event driven programming python event driven programming](https://i.pinimg.com/originals/82/45/b2/8245b285b826e9c3f42e30d03227ff78.jpg)
Turtle () # Create our favorite turtle # The next four functions are our "event handlers". bgcolor ( "lightgreen" ) # Set the background color tess = turtle. title ( "Handling keypresses!" ) # Change the window title wn. Screen () # Get a reference to the window wn. setup ( 400, 500 ) # Determine the window size wn = turtle.