Top Banner
EVENT-DRIVEN PROGRAMMI NG
32

EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน...

Jan 02, 2016

Download

Documents

Phebe Greer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

EVENT-DRIVEN PROGRAMMING

Page 2: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

EVENT-DRIVEN PROGRAMMING

• โปรแกรมและอุปกรณ์�ส่�วนมากที่��ใช้�ในช้�ว�ตประจำ�า ว�น จำะตอุบส่นอุงก�บเหตการณ์�ที่��เก�ดขึ้#$น

• ต�วอุย่�างขึ้อุงเหตการณ์� อุาที่� การเคล'�อุน หร'อุคล�กเมาส่�

Page 3: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Keypress Events

Page 4: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Press Up Key

Page 5: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Press Left Key

Page 6: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

listen method

• We need the call to the window’s listen method, otherwise it won’t notice our keypresses.

• We named our handler functions h1, h2 and so on, but we can choose better names.

• The handlers can be arbitrarily complex functions that call other functions, etc

Page 7: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

• Pressing the q key on the keyboard ca lls function h4 (because we bound th

e q key to h4 ).• 4Whileexecutingh, thewindow’sbyemet hod cl oses t he t ur t l e wi nd

ow, which causes the window’s mainl cccc cc ccc ccc cccccccccc.

Page 8: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

• We can refer to keys on the keyboard by th eir character code (as we did in the

program),

or by their symbolic names. Some of the sy mbolic names to try are

Cancel, BackSpace, Tab, Enter, Shift, Control, Alt, Pause,Caps_Lock,Escape,PageUp,PageDownEnd,Home,Left,Up,Right,Down,Print,Insert,Del et e, F1 , F2 , F3 , F4 , F5 , F6 7 8 9 10 11 12, F , F , F , F , F , F , Num_Lock, and Scroll

_Lock.

Page 9: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Mouse events

• A mouse event is a bit different from a keypress event because its handler needs two parameters to receive x,y coordinate information telling us where the mouse was when the event occurred.

Page 10: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 11: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 12: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

• There is a goto method, this allows us to move the turtle to an absolute coordinate position. (Most of the examples that we’ve seen so far move the turtle relative to where it currently is). So what this program does is move the turtle (and draw a line) to wherever the mouse is clicked. Try it out!

Page 13: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 14: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

• wn.title("Got click at coords {0}, {1}".format(x, y))• Because we can easily change the text in the wi

ndow’s title bar, it is a useful place to display

occasional debugging or status information. (Of course, this is not the real purpose of the

window title!)

But there is more!

Page 15: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Turtle handlers

• Not only can the window receive mouse events: individual turtles can also have their own

handlers for mouse clicks. The turtle that “receives” the click event will be the one under the mouse.

• So we’ll create two turtles. Each will bind a handler to its own onclick event. And

the two handlers can do different things for their turtles.

Page 16: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 17: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 18: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 19: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 20: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Automatic events from a timer

• Alarm clocks, kitchen timers, and thermonuclear bombs in James Bond movies are set to create an “automatic” event after a certain interval.

• The turtle module in Python has a timer that can cause an event when its time is up.

Page 21: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 22: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 23: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

state machines

• A state machine is a system that can be in one of a few different states.

• We draw a state diagram to represent the machine, where each state is drawn as a circle or an ellipse.

• Certain events occur which cause the system to leave one state and transition into a different state.

• These state transitions are usually drawn as an arrow on the diagram.

Page 24: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

State machines

• This idea is not new: when first turning on a cellphone, it goes into a state which we could call “Awaiting PIN”. When the correct PIN is entered, it transitions into a different state—say “Ready”. Then we could lock the phone, and it would enter a “Locked” state, and so on.

Page 25: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

State machines• c cccccc ccccc ccccccc cccc cc ccccccccc ccccc cc c ccccccc cc

ght. Here is a state di agram which shows t

ccc ccc c cccccc cccc inually cycles throug

h three different stat es, which we’ve num

bered 0 , 1 and 2 .

Page 26: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Traffic lights

We’re going to build a program that uses a turtle to simulate the traffic lights. There are three lessons here.

• The first shows off some different ways to use our turtles.

• The second demonstrates how we would program a state machine in Python, by using a variable to keep track of the current state, and a number of different if statements to inspect the current state, and take

• the actions as we change to a different state.• The third lesson is to use events from the keyboard

to trigger the state changes.

Page 27: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 28: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

A kind of state machine

• A traffic light is a kind of state machin e with three states, Green, Orange, R

.• We number these states 0 , 1 , 2• When the machine changes state, we

change tess’ position and her fillcolor.• cccc cccccccc ccccc ccc ccccccc ccccc

ccccccccc c c_=0

Page 29: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 30: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่

Press the Space bar key to change to a different state

Page 31: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่
Page 32: EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่