【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 ...
随机推荐
- 低版本IE浏览器 input元素出现叉叉的情况
都说是IE10之上的浏览器才有这个问题,恰巧我IE10之上都没有问题,反而是低版本的浏览器出现了这个问题.作为一个凭证,我先放一张图片在这里面. 之前无意中解决过这个问题,如今复现确实是没有解决,网上 ...
- COCOS2D 释放资源的最佳时机
有场景A跟场景B,场景A是当前场景,场景B是将要替换的新场景. 那么A场景的资源释放最佳时机是在什么时候呢? 这是释放资源的代码(注意要按这个顺序释放): 1 2 3 4 CCAnimationCac ...
- 真机调试 —— An unknown error occurred.
iOS开发中,总会遇见各种各样的问题.今天我就在真机调试的时候出现 An unknown error occurred. 不知道什么鬼,百度一下,各种胡说八道. 解决办法: 1.退出Xcode,重新运 ...
- apache 访问出现403 Forbidden
在linux虚拟机的apache上新增一个虚拟目录/var/wordpress,想把理论网挂上去. 在配置文件httpd.conf中,把”Include conf/extra/httpd-vhosts ...
- Effective Java 读书笔记之一 创建和销毁对象
一.考虑用静态工厂方法代替构造器 这里的静态工厂方法是指类中使用public static 修饰的方法,和设计模式的工厂方法模式没有任何关系.相对于使用共有的构造器来创建对象,静态工厂方法有几大优势: ...
- [BZOJ1503][NOI2004]郁闷的出纳员
[BZOJ1503][NOI2004]郁闷的出纳员 试题描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是 ...
- 160809209_李梦鑫_C语言程序设计实验2 选择结构程序设计
实验2-1 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 源码:#include <stdio.h> int main() { i ...
- 跟着百度学PHP[4]-OOP面对对象编程-2-属性和方法
简单的说 变量就是成员属性函数就是成员方法(方法有三:构造方法[即为__construct].成员方法.析构方法[__destruct]) 成员方法和成员属性都是可以加修饰词.比如封装性的方法或者属性 ...
- PYTHON seek()tell()语句
print(f.tell()) # 显示当前位置 f.seek(0) #回到某一起点
- Triangle
动态规划 int minimumTotal (vector<vector<int>>& triangle) { ; i >= ; --i) ; j < i ...