【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

  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()
时间: 2024-10-10 06:06:25

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

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game

Mini-project description — “Guess the number” game One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock

Mini-project description — Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw” one of three hand signals that correspond to rock, paper or s

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —"Stopwatch: The Game"

Mini-project description - "Stopwatch: The Game" Our mini-project for this week will focus on combining text drawing in the canvas with timers to build a simple digital stopwatch that keeps track of the time in tenths of a second. The stopwatch

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

【Python】如何在Windows操作系统下安装Python和Networkx

Networkx是一套基于Python的多种网络构造库.因为之前没有学过Python,因此一点点上手,这一篇讲一讲如何在Windows环境下安装Python2.7和Networkx. 首先要澄清一下,如果是想深入系统学习Python的同学,还是尽早换Linux系统,因为Windows底下的库安装非常麻烦:而Linux底下只需要运行命令行(Terminal): sudo apt-get install python-matplotlib 就可以了. 由于仅仅是使用Networkx构造数据的关系,以

【转载】在Ubuntu下配置舒服的Python开发环境

在Ubuntu下配置舒服的Python开发环境 2013-10-26 00:10 11188人阅读 评论(0) 收藏 举报 目录(?)[+] Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的一个安装和配置步骤,基于 Ubuntu 12.04 桌面版本标准安装. 安装 Python 发布版本和 build 依赖包 建议至少安装 Python 2.7/3.2 版本,毕竟 Python 2.X/3.