Fu Fundamentals of Pr Programming (Py Python) Event-Driven Programming Ali Taheri Sharif University of Technology Spring 2019
Outline 1. Introduction to Turtle Module 2. Handling Keypress Events 3. Handling Mouse Events 4. Handling Automatic Events 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Introduction to Turtle Module The turtle module provides a basic method to draw shapes and patterns ◦ You need to install the package using pip. import turtle # Allows us to use turtles window = turtle.Screen() # Creates a playground for turtles marker = turtle.Turtle() # Create a turtle, assign to marker marker.forward(50) # Tell marker to move forward by 50 units marker.left(90) # Tell marker to turn by 90 degrees marker.forward(30) # Complete the second side of a rectangle window.mainloop() # Wait for user to close window 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Introduction to Turtle Module 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Introduction to Turtle Module Customizing turtle import turtle window = turtle.Screen() marker = turtle.Turtle() marker.speed(0) # Set the speed of marker colors = [ 'red' , 'purple' , 'blue' , 'green' , 'yellow' , 'orange' ] for x in range(360): marker.color(colors[x % 6]) # Set the color of marker marker.pensize(x / 100 + 1) # Set marker's width marker.forward(x) marker.left(59) window.mainloop() 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Introduction to Turtle Module 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Handling Keypress Events Using turtle’s onkey() method ◦ It binds a callback function to a specific key import turtle window = turtle.Screen() # Get a reference to the window window.setup(400, 500) # Determine the window size window.title( "Handling keypresses!" ) # Change the window title window.bgcolor( "lightgreen" ) # Set the background color marker = turtle.Turtle() # Create our favorite turtle # The next four functions are our "event handlers". up_handler = lambda : marker.forward(30) left_handler = lambda : marker.left(45) right_handler = lambda : marker.right(45) q_handler = lambda : window.bye() # Close down the turtle window # These lines "wire up" keypresses to the handlers we've defined. window.onkey(up_handler, "Up" ) window.onkey(left_handler, "Left" ) window.onkey(right_handler, "Right" ) window.onkey(q_handler, "q" ) window.listen() # tell the window to start listening for events window.mainloop() 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Handling Mouse Events Using turtle’s onclick() method ◦ It binds a callback function to mouse click key event import turtle window = turtle.Screen() window.title( "How to handle mouse clicks on the window!" ) window.bgcolor( "lightgreen" ) marker = turtle.Turtle() marker.color( "purple" ) marker.pensize(3) marker.shape( 'circle' ) def right_click_handler(x, y): marker.penup() marker.goto(x, y) marker.pendown() window.onclick(marker.goto) window.onclick(right_click_handler, 3) window.mainloop() 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Handling Automatic Events Using turtle’s ontimer() method import turtle from random import random, randint window = turtle.Screen() window.setup(800, 600) marker = turtle.Turtle() def timer_handle(): marker.penup() x, y = randint(-400, 400), randint(-300, 300) marker.goto(x, y) marker.pendown() marker.fillcolor((random(), random(), random())) marker.begin_fill() length = randint(10, 100) for i in range(4): marker.forward(length) marker.right(90) marker.end_fill() window.ontimer(timer_handle, 1000) window.ontimer(timer_handle, 1000) window.mainloop() 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Recommend
More recommend