Some insights into Python learning

Source: Internet
Author: User

First of all, the library used here is a library created by the teacher when learning Coursera.

#timerimport simpleguiimport random# Global statemessage = "Python is fun!" Position = [50]width = 500height = 500interval = 2000#handler for Text boxdef update (text):    global Message    mes Sage = text    #Handler for Timerdef tick ():    x = Random.randrange (0, width)    y = random.randrange (0, height)    Position[0] = x    position[1] = y    #Handler for draw on Canvasdef draw (canvas):    canvas.draw_text (Message, Position, "Yellow")    #Create a frameframe = Simplegui.create_frame ("Home", width, height) #Register event Handlertext = Frame.add_input ("Message", UPDATE, Max) Frame.set_draw_handler (draw) timer = Simplegui.create_timer ( Interval, tick) Frame.start () Timer.start ()

First look at the above code, is a simple screen saver

Some of the ideas raised by the above code:

Python programming Now I think one of the biggest features of it is to put
Events are divided into several unrelated child events.
For example this program:
A simple screen saver:
Set the message you want to display as an event
Set a timed change position to an event
You can also set the time interval to an event if you can.
Finally, paint is set to an event.
Appropriate decomposition procedures

The point is irrelevant!!

In addition, in class also found a problem, although found the answer, the teacher also gave a brief answer

Question: Why change the variable position without declaring
Global
Answer: Because it changes the position element in turn, and
Did not change position itself

Some insights into Python learning

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.