Week 2a - Interactive Application in Python
Event-driving programming
defination
Initialize everything and waite events to run the program
What is event
- Input
- button
- text box
- Keyboard
- key down
- key up
- Mouse
- click
- drag
- Timer
To create a timer:
timer = simplegui.create_timer(intervel, timer_handler)
# 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()
Event queue
- solve one event at one time
- we don’t write event queue
analogy: like hollywood, if u pass the interview, interviewer will call u, u shouldn’t call back
Local vs. Global Variables
If u didn’t declare a varible in function is a global variable, it will defaulted as a local varible.
But one thing is allowed: that is use the value of global varible (except ==
)
num1 = 1
print num1
a = 2
# num2 is a local variable
def fun():
num2 = num1 + 1
print num2
if a == 2:
print a + 1
fun()
If we want to use global variable and change it
# example
num = 4
def fun1():
global num
num = 5
def fun2():
global num
num = 6
# note that num changes after each call with no obvious explanation
print num
fun1()
print num
fun2()
print num
SimpleGUI
Definition
SimpleGUI is the python margin and code sculpter that allow us to build interactive applications.
Frame
- Control Area
- Canvas
- State Area
To creat a frame:
simplegui.create_frame(title, canvas_width, canvas_height)
simplegui.create_frame(title, canvas_width, canvas_height, control_width)
Recommand Order of Program
- global(state)
- ‘helper’ function
- classes
- define event handlers
- create a frame
- register event handlers
- start frame & timer
# 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
# Create a frame
frame = simplegui.create_frame("SimpleGUI Test", 100, 100)
# Register event handlers
timer = simplegui.create_timer(1000, tick)
# Start frame and timers
timer.start()
Week 2b - Buttons and Input Fields
Swap
We can use code like a, b = b, a
Button
To add a button:
frame.add_button(text, button_handler)
frame.add_button(text, button_handler, width)
Input Fields
frame.add_input(text, input_handler, width)
The input is string rather than int
# 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()
Code Style
Now that you are about to write longer, more complex pieces of Python, it is a good time to talk about coding style. Most languages can be written (or more concise, formatted) in different styles; some are more readable than others. Making it easy for others to read your code is always a good idea, and adopting a nice coding style helps tremendously for that.
For Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:
Use 4-space indentation, and no tabs.
4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.Wrap lines so that they don’t exceed 79 characters.
This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.Use blank lines to separate functions and classes, and larger blocks of code inside functions.
When possible, put comments on a line of their own.
Use docstrings.
Use spaces around operators and after commas, but not directly inside bracketing constructs:
a = f(1, 2) + g(3, 4)
.Name your classes and functions consistently; the convention is to use
CamelCase
for classes andlower_case_with_underscores
for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).Don’t use fancy encodings if your code is meant to be used in international environments. Plain ASCII works best in any case.