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