Python学习笔记(3)事件驱动模型,frame, button

Codecademy这个在线学习平台上提供的Python教程非常友好,让初学者非常容易上手,而且每练习一些题目,网站还给你发个奖章鼓励鼓励你继续进行下去,我觉得这个网站做得挺用心的。

这个网站的练习做完了,开始学习Coursera上的Python课程了,这个课程的最终目标是让你可以编一个飞船射击的游戏,更加侧重网页版的GUI编程。虽然我对这个GUI并不太感兴趣,不过也注册了这个课程,就当做继续练习Python语法了吧。

下面的笔记内容来自coursera上的Python公开课

Event-driven Programming model
keep waiting forever until an event occur.

                              event1….event999
Start  — initiate — wait
                              handler1…handler999


Event:
1 keyboard
2 input
3 mouse
4 timer


Example of a simple event-driven program

import simplegui

# Event handler
def tick():
    print "tick!"

# Register handler
timer = simplegui.create_timer(1000, tick)

# Start timer
timer.start()


Event Queue(好莱坞模式)
例如,What happens if you press a key and click the mouse at exactly the same time?
那么,Event Queue管理机制将会使得,One of the event handlers executes and the other waits in the event queue until the first handler finishes.


Local and Global Variables

1 尽可能地使用local variables
2 global variables的好处在于,global variables are an easy way for event handlers to communicate game information.但是更好的办法是使用 OOP

# global vs local examples

# num1 is a global variable

num1 = 1
print num1

# num2 is a local variable

def fun():
    num1 = 2  
    #这里的num1虽然和line 5的全局变量num1是同名的,但是其实本质是local variable。在function()中的变量都是local variable
    num2 = num1 + 1
    print num2
   
fun()

# example
num = 4

def fun1():
    global num
    num = 5
   
def fun2():
    global num
    num = 6

# note that num changes after each call with no obvious explanation   
print num
fun1()
print num
fun2()
print num


GUI Programme Structure(7 steps)
1 define globals.
2 helper functions
3 class
4 define event handlers
5 create a frame
6 register event handlers
7 start the frame and timers
# SimpleGUI program template

# Import the module
import simplegui

# Define global variables (program state)
counter=0

# Define "helper" functions
def increment():
    global counter
    counter=counter+1

# Define event handler functions
def tick():
    increment()
    print counter
def buttionpress():
    global counter
    counter=0

# Create a frame
frame=simplegui.create_frame("SimpleGui Test", 100, 100)

# Register event handlers
timer=simplegui.create_timer(1000,tick)
frame.add_button("click me",buttionpress)

# Start frame and timers
frame.start()
timer.start()


Frame
A frame is a window, which is a container for the controls, status information, and canvas. A program can create only one frame

Text Input Box
0  the input fields event is called when you press enter on the input field.
1  Input fields pass data that is typed into the control area to designated event handlers in the program. 
2  It is important to note that this information is always given as a string.
3  You get an error if you try to create the button before you define the event handler, 
4  You get an error if you use a handler with the wrong number or type of parameters.
5  Input fields allow the user to give data to the program.

# calculator with all buttons

import simplegui

# intialize globals
store = 0
operand = 0


# event handlers for calculator with a store and operand

def output():
    """prints contents of store and operand"""
    print "Store = ", store
    print "Operand = ", operand
    print ""
   
def swap():
    """ swap contents of store and operand"""
    global store, operand
    store, operand = operand, store
    output()
   
def add():
    """ add operand to store"""
    global store
    store = store + operand
    output()

def sub():
    """ subtract operand from store"""
    global store
    store = store - operand
    output()

def mult():
    """ multiply store by operand"""
    global store
    store = store * operand
    output()

def div():
    """ divide store by operand"""
    global store
    store = store / operand
    output()

def enter(t):
    """ enter a new operand"""
    global operand
    
     #错误1 没有将输入字符串转换为数字型
     #operand = t
   
     #错误2 如果将字符串转为int型,将不能接受float型输入
     #operand = int(t)
    
     #正确写法
     operand = float(t)
     output()
   
# create frame
f = simplegui.create_frame("Calculator",300,300)

# register event handlers and create control elements
f.add_button("Print", output, 100)
f.add_button("Swap", swap, 100)
f.add_button("Add", add, 100)
f.add_button("Sub", sub, 100)
f.add_button("Mult", mult, 100)
f.add_button("Div", div, 100)
f.add_input("Enter", enter, 100)

# get frame rolling
f.start()

For, debugging, you should remember that building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值