【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 ...
随机推荐
- 大数据BI积累
http://blog.csdn.net/wyzxg/article/category/535869 设计论文:http://www.doc88.com/p-3877368345851.html 自动 ...
- jquery隐藏按钮
$(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...
- bootstrap搜索框样式代码及效果
<div class="container"> <div class="input-group"> <input type=&qu ...
- hdu.1044.Collect More Jewels(bfs + 状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 如何查看华为EMUI系统APK源码?
最近想看一下华为EMUI里面的某些系统APK是如何实现的. 那如何获取系统APK呢? 有两种方式: 1.安装豌豆荚,豌豆荚里有一个应用管理的功能,可以查看手机里的所有应用,包括系统应用. 可以使用该功 ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- hash-2.hashMap
1.HashMap的数据结构 a.HashMap是一个链表散列的结合体,即,数组和链表的结合体. b.HashMap类中定义了Entry类型的数组,Entry [ ] ,Entry有key value ...
- CK-Editor content.replace
<s:property value="content.replace('\n\r', '<br/>')" escape="false"/> ...
- canvas对象arc函数的使用-遁地龙卷风
(-1)写在前面 我用的是chrome49 <canvas id="lol" height="300"></canvas> (1)详细介 ...
- Android学习笔记(十四)——自定义广播
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们除了可以通过广播接收器来接收系统广播, 还可以在应用程序中发送自定义的广播.下面我们来分别试一试发送自定义 ...