Python学习笔记(5)Timer
来源:互联网 发布:linux sort -k 1 编辑:程序博客网 时间:2024/06/07 19:58
下面的笔记内容来自coursera上的Python公开课。
I have a tick handler that handles changing the position of the text and
I have a draw handler that simply draws whatever message at whatever position.
These things can run at completely different rates, alright?
The draw handler runs at 60 times a second, and
The tick handle runs once every two seconds, and
The update handle runs only whenever you type touch it, okay?
But it still all works together nicely to give us this interesting interactive application.
例2 timer 设定了interval后如何改变interval呢
但是运行时发现,其实这个改动并没有生效。
正确的改动interval的方法是这样的:
A good program design principle:
I have an update handler that handles changing the message, andI have a tick handler that handles changing the position of the text and
I have a draw handler that simply draws whatever message at whatever position.
These things can run at completely different rates, alright?
The draw handler runs at 60 times a second, and
The tick handle runs once every two seconds, and
The update handle runs only whenever you type touch it, okay?
But it still all works together nicely to give us this interesting interactive application.
例1 Simple "screensaver" program
# Import modulesimport simpleguiimport random# Global statemessage = "Python is Fun!"position = [50, 50]width = 500height = 500interval = 2000# Handler for text boxdef update(text): global message message = text # Handler for timerdef tick(): x = random.randrange(0, width) y = random.randrange(0, height) position[0] = x position[1] = y# Handler to draw on canvasdef draw(canvas): canvas.draw_text(message, position, 36, "Red")# Create a frameframe = simplegui.create_frame("Home", width, height)# Register event handlerstext = frame.add_input("Message:", update, 150)frame.set_draw_handler(draw)timer = simplegui.create_timer(interval, tick)# Start the frame animationframe.start()timer.start()
例2 timer 设定了interval后如何改变interval呢
#下面这样改可以吗?import simpleguidef timer_handler(): print "timer called"timer = simplegui.create_timer(50, timer_handler)timer.start()timer = simplegui.create_timer(1000, timer_handler)timer.start()上面代码中,timer的interval企图从50 milisecond变化为1second(1000 milisecond)。
但是运行时发现,其实这个改动并没有生效。
正确的改动interval的方法是这样的:
…timer.stop()timer = simplegui.create_timer(1000, timer_handler)timer.start()
0 0
- Python学习笔记(5)Timer
- java Timer学习笔记
- java Timer学习笔记
- STM32CUBEMX TIMER 学习笔记
- Timer学习笔记
- Python学习笔记(5)
- python学习笔记5
- python学习笔记5
- Python 学习笔记 - 5
- Python 学习笔记 5
- python 学习笔记5
- Python学习笔记5
- python 学习笔记5
- Python 学习笔记(5)
- Python学习笔记5
- Python 学习笔记5
- python学习笔记5
- Python学习笔记--5
- 程序员学习能力提升三要素(时刻提醒自己)
- iOS 开发者计划申请 2014 年最新总结
- word2010中,插入-符号-公式显示是灰色的解决办法
- hdu 4586 Play the Dice(数论)
- Keep Walking In MongoDB 1
- Python学习笔记(5)Timer
- 浙江“一男娶两女”当事人谈婚礼真相
- Android中创建和使用数据库
- 无论你的日子有多么糟糕,总有一些小美好出现。
- 湖南双峰县发红头文件为嫌犯请求取保候审
- vs2010中臃肿的ipch和sdf文件[紫龍劍]
- 剑指offer:从上往下打印二叉树
- C++字符串常量跨平台编译问题(与字符串编码相关)
- 简单工厂模式