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