Plan for today and next time
• Today: wxPython (part 1)
CSE 399-004: • Aside: Arguments to functions
• Whirlwind tour of the widgets
Python Programming • Next time: wxPython (part 2)
• XRCed (?)
Lecture 08: Graphical User Interfaces with wxPython • Event handling
March 12, 2005 • Email me by Friday with ideas for something I can
build during lecture
http://www.seas.upenn.edu/~cse39904/
Default arguments (Part 1)
>>> i = 10
If no value is supplied
>>> def f(arg = i):
... print arg
for arg, use i.
Arguments to functions ...
The value of the default argument
>>> i = 20
(Tutorial 4.7) >>> f() is evaluated at the point when the
10 function is defined.
>>> f(15)
15
Default arguments (Part 2) Keyword arguments (Part 1)
Arguments with default
>>> def f(elt, arg = []): values must come at >>> def f(foo, bar, baz = 'baz'):
... arg.append(elt) the end of the ... print foo, bar, baz
... return arg arguments list. ...
... We can give the
>>> f(bar = 2, foo = 3)
>>> f(1)
3 2 baz values for a functions
[1]
>>> f(foo = 1, bar = 2) arguments by name.
>>> f(2)
1 2 baz
[1, 2]
If a default value is mutable, its >>> f(1, bar = 3)
>>> f(3) We can mix positional
value persists across calls. 1 3 baz
[1, 2, 3] arguments with keyword
arguments, but keyword
arguments must come last.
5 6
Keyword arguments (Part 2) General rules for calling functions
• Positional arguments come before keyword arguments
>>> def f(foo, bar = 'bar', baz = 'baz'):
... print foo, bar, baz • The value of each argument must be given at most once
...
>>> def f(a = 2):
>>> f(1)
... print a
1 bar baz
...
>>> f(2, baz = 3)
>>> f()
2 bar 3
2
Using keyword arguments lets >>> f(10)
10
us give values to arguments
>>> f(a = 20)
which have default values.
20
7 8
General rules for calling functions General rules for calling functions
• Positional arguments come before keyword arguments • Positional arguments come before keyword arguments
• The value of each argument must be given at most once • The value of each argument must be given at most once
>>> def f(a = 2): >>> def f(a = 2):
... print a ... print a
... ...
>>> f(a = 30, 0) >>> f(0, a = 30)
File "<stdin>", line 1 Traceback (most recent call last):
SyntaxError: non-keyword arg after keyword arg File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword
argument '
9 10
Arbitrary argument lists Arbitrary argument lists
>>> def f(arg, bar = 3, *rest, **keywords): >>> def f(arg, bar = 3, *rest, **keywords):
... print arg, bar, rest, keywords ... print arg, bar, rest, keywords
... ...
>>> f(1) f has one required argument >>> f(1)
1 3 () {} 1 3 () {} Extra positional arguments are put
>>> f(1, 2) f has two positional arguments >>> f(1, 2) in a tuple and passed in as rest.
1 2 () {} 1 2 () {}
>>> f(1, 2, 3, 4) >>> f(1, 2, 3, 4)
1 2 (3, 4) {} 1 2 (3, 4) {}
>>> f(1, a = 2, b = 3) >>> f(1, a = 2, b = 3)
1 3 () {'a': 2, 'b': 3} 1 3 () {'a': 2, 'b': 3}
>>> f(1, 2, 3, 4, a = 5, b = 6) >>> f(1, 2, 3, 4, a = 5, b = 6)
1 2 (3, 4) {'a': 5, 'b': 6} 1 2 (3, 4) {'a': 5, 'b': 6}
11 12
Arbitrary argument lists Arbitrary argument lists
>>> def f(arg, bar = 3, *rest, **keywords): >>> def f(arg, bar = 3, *rest, **keywords):
... print arg, bar, rest, keywords ... print arg, bar, rest, keywords
... ...
>>> f(1) >>> f(1)
1 3 () {} 1 3 () {}
>>> f(1, 2) Extra keyword arguments are >>> f(1, 2)
1 2 () {} put in a dictionary and passed 1 2 () {}
>>> f(1, 2, 3, 4) in as keywords. >>> f(1, 2, 3, 4)
1 2 (3, 4) {} 1 2 (3, 4) {}
>>> f(1, a = 2, b = 3) >>> f(1, a = 2, b = 3)
1 3 () {'a': 2, 'b': 3} 1 3 () {'a': 2, 'b': 3}
>>> f(1, 2, 3, 4, a = 5, b = 6) >>> f(1, 2, 3, 4, a = 5, b = 6)
1 2 (3, 4) {'a': 5, 'b': 6} 1 2 (3, 4) {'a': 5, 'b': 6}
13 14
wxPython
• http://www.wxpython.org/
• A set of bindings to the cross-platform wxWidgets GUI
library (written in C++)
wxPython • Claim: Makes it easy to develop graphical user interfaces
(GUIs) for applications in Python
16
Tkinter vs. wxPython Resources
• Tkinter and wxPython are both GUI libraries for Python • Online API documentation:
http://wxpython.wxcommunity.com/docs/api/
• Tkinter:
• Comes with the standard Python distribution • Wiki:
• Non-native look and feel http://wiki.wxpython.org/index.cgi
• Cumbersome to build complex UIs
• "Stale" • The demo application that comes with wxPython
• Has demos of all the widgets
• wxPython: • Includes the source code for the demos
• Not part of standard Python distribution
• Native look and feel
• Easier to work with than Tkinter
• "Up and coming" 17 18
Resources Event handling
• Tutorial: • GUIs are event driven: They sit around doing nothing
http://wiki.wxpython.org/index.cgi/Getting_Started until the user clicks a button, moves the mouse, etc.
• Tutorial: • You, the programmer, are responsible for indicating how
http://wiki.wxpython.org/index.cgi/AnotherTutorial events are to be handled
• You are not responsible for polling for events
• The second is a bit better (in my opinion)
• Neither does that great a job explaining event handling
• The style of programming here is quite different from
• My suggestion: Work by example normal sequential programming
19 20
Some terminology
• A window is anything that consumes screen space
• A frame is what you normally think of as a "window"
21