Creating Thick Client Apps Quickly with PythonCard
by Ralph Heimburger
September 17, 2009
|
Need to build your Python app fast? PythonCard might hold
the answers you seek.
|
Introduction
PythonCard is a GUI construction toolkit written entirely
in Python that offers a cross-platform rapid GUI development
with an intuitive interface. Inspired by Apple's Hypercard,
PythonCard separates the GUI components from the event
driven code.
When I first found PythonCard I was looking for a rapid
application GUI toolkit that followed some of the design
promises of Python, one being "it gets out of your way and
allows you to code". I quickly found that this GUI toolkit
offered a very intuitive interface and I was able to quickly
write functional applications for a variety of purposes.
PythonCard comes with very good code samples that allow
developers to quickly get up and running.
Getting Started
To install PythonCard, there are some requirements.
First, you will need Python itself, version 2.5 or higher,
then wxPython, which is the underlying library which
PythonCard runs on. And lastly, of course is PythonCard.
Note: If you are developing on a Windows platform you
will find installers for each of these.
Creating your first PythonCard application
The first step in creating any application is to
determine it's basic functionality. Although PythonCard
offers a lot of basic components, it may not fit your
applications' design goals, so consider the audience of your
application before deciding which toolkit to use.
Using the Resource Editor
The GUI construction component of PythonCard is called
the Resource Editor. When you open it you will see a basic
empty template where you can begin entering components, e.g.
TextFields, Buttons, Lists, etc. The Resource Editor has 2
screens, the Resource Editor and the Property Editor. The
Property Editor allows you to modify the properties (size,
color, font, command, etc) of the components you add via the
Resource Editor. The components PythonCard exposes are a
subset of the wxPython library. PythonCard does not restrict
you from going beyond these basic controls, but it gives you
a rich set of the basic library. Again, PythonCard is a
Rapid GUI construction toolkit and intended to be so.
If this is a new application, the Resource Editor will
save 2 files for your application, one being the application
main python file and a resource file. Assume that your
application is named myapp, you will see myapp.py and
myapp.rsrc.py. This is by design and allows you to focus
your development and testing through myapp.py and changing
GUI components via myapp.rsrc.py.
Resource Editor:
Property Editor (ClickMe button)
First look at the Resource Editor generated code.
#!/usr/bin/python
"""
__version__ = "$Revision: 1.5 $"
__date__ = "$Date: 2004/04/30 16:26:12 $"
"""
from PythonCard import model
class MyBackground(model.Background):
def on_initialize(self, event):
# if you have any initialization
# including sizer setup, do it here
pass
if __name__ == '__main__':
app = model.Application(MyBackground)
app.MainLoop()
PythonCard creates the "shell" for the GUI application
that when invoked from the command prompt by typing
'myapp.py', will essentially do nothing because there are no
event handlers coded yet.
Adding the event code for the ClickMe button
I decided that when the ClickMe button is clicked, I want
to respond with a dialog box. Again, this is an introductory
example to demonstrate and introduce PythonCard. I bolded
the code sample below where I added the additional code.
#!/usr/bin/python
"""
__version__ = "$Revision: 1.5 $"
__date__ = "$Date: 2004/04/30 16:26:12 $"
"""
from PythonCard import model, dialog
class MyBackground(model.Background):
def on_initialize(self, event):
# if you have any initialization
# including sizer setup, do it here
pass
def on_ClickMe_command(self, event):
about="This application now works.\nyou clicked Me!"
dlg = dialog.scrolledMessageDialog(self, about, 'ClickMe')
return
if __name__ == '__main__':
app = model.Application(MyBackground)
app.MainLoop()
How it looks
Summary
The completed myapp.py contains 25 lines of highly
readable and maintainable code. The resource or 'rsrc' file
is 44 lines. PythonCard apps can also be compiled to
executables using either Py2exe or
cx_freeze so they can be distributed as
standalone GUI applications.
|