【python】An Introduction to Interactive Programming in Python(week two)
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
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)的更多相关文章
- 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 ...
- An Introduction to Interactive Programming in Python
这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...
- 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 ...
随机推荐
- 360随身WIFI程序单文件绿色版及网卡驱动(附使用感受)
大家好,我是Colin,今天刚收到传说中的360WIFI,拿到手后马上就进行了测试.就做工而言,19.9的价格算是比较公道的,网卡很小,做工还可以,带磨砂质感,而且还提供了一个耳机插头,可以当挂件一样 ...
- ios修改textField的placeholder的字体颜色和大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- 安装wampserver 2.5的时候出现丢失MSVCR100.dll的解决办法。
转载地址:http://www.mafutian.net/127.html
- 使一个div始终显示在页面中间
使一个div始终显示在页面中间 假设我们有一个div层:<div id=”myDiv”></div> 首先,我们用css来控制它在水平上始终居中,那么我们的css代码应该是这样 ...
- 【C语言入门教程】2.4 浮点型数据
浮点型数据又称实型数据,是一个以十进制表示的符号实数.符号实数的值包括整数部分.尾数部分和指数部分. 2.4.1 浮点型常量 一些较大的数值,或者有小数位.指数位的数值都需要用浮点型常量表示.浮点型常 ...
- 【C语言入门教程】5.1 函数说明 与 返回值
C 语言是结构化语言,它的主要结构成分是函数.函数被作为一种构件,用以完成程序中的某个具体功能.函数允许一个程序的各个任务被分别定义和编码,使程序模块化.本章介绍 C 语言函数的设计,如何用函数分解程 ...
- BNR Android Demo学习笔记(一)——CrimeIntent
开发环境:win7,Android Studio 1.2, 1.Model Crime,数据模型,每个Crime有一个UUID作为唯一标识. package tina.criminalintent; ...
- springmvc之前后台传值
一.向后台传值 1.项目结构 2.jar包 3.spring-config.xml <?xml version="1.0" encoding="UTF-8" ...
- jQuery - 动态创建iframe并加载页面
<html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...
- flexbox-CSS3弹性盒模型flexbox完整版教程
原文链接:http://caibaojian.com/flexbox-guide.html flexbox-CSS3弹性盒模型flexbox完整版教程 A-A+ 前端博客•2014-05-08•前端开 ...