Event-Driven Programming Ali Taheri Sharif University of Technology - - PowerPoint PPT Presentation

event driven programming
SMART_READER_LITE
LIVE PREVIEW

Event-Driven Programming Ali Taheri Sharif University of Technology - - PowerPoint PPT Presentation

Fu Fundamentals of Pr Programming (Py Python) Event-Driven Programming Ali Taheri Sharif University of Technology Spring 2018 Outline 1. Introduction to Turtle Module 2. Handling Keypress Events 3. Handling Mouse Events 4. Handling


slide-1
SLIDE 1

Fu Fundamentals of Pr Programming (Py Python)

Event-Driven Programming

Ali Taheri Sharif University of Technology

Spring 2018

slide-2
SLIDE 2

Outline

  • 1. Introduction to Turtle Module
  • 2. Handling Keypress Events
  • 3. Handling Mouse Events
  • 4. Handling Automatic Events

2

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

slide-3
SLIDE 3

Introduction to Turtle Module

The turtle module provides a basic method to draw shapes and patterns

  • You need to install the package using pip.

3

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

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

slide-4
SLIDE 4

Introduction to Turtle Module

4

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

slide-5
SLIDE 5

Introduction to Turtle Module

Customizing turtle

5

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

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()

slide-6
SLIDE 6

Introduction to Turtle Module

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

slide-7
SLIDE 7

Handling Keypress Events

Using turtle’s onkey() method

  • It binds a callback function to a specific key

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

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()

slide-8
SLIDE 8

Handling Mouse Events

Using turtle’s onclick() method

  • It binds a callback function to mouse click key event

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

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()

slide-9
SLIDE 9

Handling Automatic Events

Using turtle’s ontimer() method

9

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2018

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()