Introduction to Python Interactive programming----event-driven programming

Source: Internet
Author: User

The traditional programming is in the following linear mode:

Start---> code block a---> code block b---> block c---> Block d---...---> End

Each code block is a code that accomplishes a variety of things, but the programmer knows the code block a,b,c,d ... Order of execution, the only thing that can change this process is data. Enter different data, judging by the conditional statement, the process may be changed to a--->c--->e ...---> End. Each time the program may run in a different order, but its control flow is determined by the input data and the program you are writing. If you know the current running state of the program (including the input data and the program itself), then you know that the next step is even until the end of its running process.

For the event-driven program model, the process is roughly as follows:

Start---> Initialize---> Wait

Unlike the traditional programming model above, the event driver is waiting there after startup, waiting for what? Wait for the event to be triggered. Traditional programming also has a "wait" time, such as in code block D, you define an input (), the user needs to enter data. But this is different from the wait for the following, traditional programming "Wait", such as input (), you as a program writer know or force the user to enter something, perhaps the number, perhaps the file name, if the user input error, you need to remind him, and ask him to re-enter. The wait for the event driver is completely unknown and does not force the user to enter or do anything. As long as an event occurs, the program will respond accordingly. These events include input information, a mouse, a key on the keyboard, and a system internal timer trigger.

Python uses the Simplegui module to implement program interaction with the following structure:

The following is a simple interactive programming program:

1 #Import the module2 ImportSimplegui3 4 #Define Global Variables ( program state)5Counter =06 7 #Define "helper" functions8 defincrement ():9     GlobalcounterTenCounter = counter + 1 One  A #Define event handler functions - deftick (): - Increment () the     Printcounter -  - #Create A Frame -frame = Simplegui.create_frame ("Test", 100,100) +  - #Register Event Handlers +Timer = Simplegui.create_timer (1000, tick) AFrame.add_button ("Click me!", tick) at  - #Start frame and timers - Frame.start () -Timer.start ()

  

Introduction to Python Interactive programming----event-driven programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.