This is a note for https://class.coursera.org/interactivepython-005

In week two, I have learned:

1.event-drvien programing

4 event types:

Input: button, textbox

Keyborad: key up, key down

Mouse: click, drag

Timer

example:

# Example of a simple event-driven program

# CodeSkulptor GUI module
import simplegui # Event handler
def tick():
print "tick!" # Register handler
timer = simplegui.create_timer(1000, tick) # Start timer
timer.start()

2. global variables

when you want to change the global variables, use global, ifelse you don't need global.(不需更改全局变量,只是想使用全局变量的值的时候,不需要声明global)

3. simpleGUI

Program structure with 7 steps:

①Globals (state)

②Helper functions

③Classes

④Define event handlers

⑤Create a frame

⑥Register event handlers

⑦Start frame & 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 buttonpress():
global counter
counter = 0 # Create a frame
frame = simplegui.create_frame("SimpleGUI Test", 100, 100)
frame.add_button("Click me!", buttonpress)
# Register event handlers
timer = simplegui.create_timer(1000, tick) # Start frame and timers
frame.start()
timer.start()

Simple Calculator

Data

  Store

  Operand

Operations

  print

  Swap

  Add

  Subtract

  Multiple

  Divide

Computation

  Store = store operation operand

# 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
operand = int(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()

【python】An Introduction to Interactive Programming in Python(week two)的更多相关文章

  1. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  2. An Introduction to Interactive Programming in Python

    这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...

  3. Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  4. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习

    #Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...

  5. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习

    # Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...

  6. Quiz 6b Question 8————An Introduction to Interactive Programming in Python

     Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...

  7. Quiz 6b Question 7————An Introduction to Interactive Programming in Python

     Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...

  8. Quiz 6a Question 7————An Introduction to Interactive Programming in Python

     First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...

  9. Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"

    Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...

随机推荐

  1. MySql的安装及配置详细指引!

    一.安装My Sql数据库 1.1,首先下载MySQL与HeidiSQL工具,双击打开后可以看到名为”mysql-5.0.22-win32 Setup.exe”的安装程序,双击执行该程序. 1.2,打 ...

  2. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  3. sql group by 理解

    order by是对字段进行排序,group by 是对字段进行分类,在select 语句中可以使用group by 子句将行划分成较小的组,然后,使用组函数返回每一个组的汇总信息,另外,可以使用ha ...

  4. 【R】如何确定最适合数据集的机器学习算法 - 雪晴数据网

          [R]如何确定最适合数据集的机器学习算法 [R]如何确定最适合数据集的机器学习算法 抽查(Spot checking)机器学习算法是指如何找出最适合于给定数据集的算法模型.本文中我将介绍八 ...

  5. “RESTful架构”相关资料收藏

    [阮一峰]理解RESTful架构 [InfoQ]深入浅出REST 用于构建 RESTful Web 服务的多层架构 REST会是SOA的未来吗? Restful 与 SOA 的关系? 回答1: 注意r ...

  6. linux远程复制和压缩文件的命令

    1.linux导出指定数据库表 mysqldump  127.0.0.1  -uname -ppwd  databasename  tablename>/home/name/tablename. ...

  7. webApp前端开发技巧总结

    自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于WEB形式的应用程序,运行在高端的移动终端设备.我相信各位童鞋应该和我一个样子, ...

  8. MongoDB的学习和使用(查询find)

    语法 MongoDB 查询数据的语法格式如下: >db.COLLECTION_NAME.find() find() 方法以非结构化的方式来显示所有文档. 如果你需要以易读的方式来读取数据,可以使 ...

  9. Unity路径规划

    Unity路径规划  转自:http://www.cnblogs.com/zsb517/p/4090629.html 背景 酷跑游戏中涉及到弯道.不规则道路. 找来一些酷跑游戏的案例来看,很多都是只有 ...

  10. uMlet建模工具

    下载:http://www.umlet.com/ 无意中发现的一款建模工具,能快速搭建数据库模型,前置安装条件是java环境. 这是我建的user模型表,2个字段name和age,2个方法getAge ...