1 <?xml version=
"1.0" encoding=
"iso-8859-1" ?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1" />
6 <meta name=
"generator" content=
"Docutils 0.3.1: http://docutils.sourceforge.net/" />
7 <title>Example Programs Using wxPython
</title>
8 <meta name=
"author" content=
"Patrick K. O'Brien" />
9 <meta name=
"organization" content=
"Orbtech" />
10 <meta name=
"date" content=
"2003-07-02" />
11 <link rel=
"stylesheet" href=
"default.css" type=
"text/css" />
14 <div class=
"document" id=
"example-programs-using-wxpython">
15 <h1 class=
"title">Example Programs Using wxPython
</h1>
16 <h2 class=
"subtitle" id=
"a-survival-guide-for-the-post-wx-prefixed-world">A survival guide for the post-wx-prefixed world.
</h2>
17 <table class=
"docinfo" frame=
"void" rules=
"none">
18 <col class=
"docinfo-name" />
19 <col class=
"docinfo-content" />
21 <tr><th class=
"docinfo-name">Author:
</th>
22 <td>Patrick K. O'Brien
</td></tr>
23 <tr><th class=
"docinfo-name">Contact:
</th>
24 <td><a class=
"first last reference" href=
"mailto:pobrien@orbtech.com">pobrien
@orbtech.com
</a></td></tr>
25 <tr><th class=
"docinfo-name">Organization:
</th>
26 <td><a class=
"first last reference" href=
"http://www.orbtech.com/">Orbtech
</a></td></tr>
27 <tr><th class=
"docinfo-name">Date:
</th>
28 <td>2003-
07-
02</td></tr>
29 <tr><th class=
"docinfo-name">Revision:
</th>
33 <div class=
"contents topic" id=
"contents">
34 <p class=
"topic-title"><a name=
"contents">Contents
</a></p>
36 <li><a class=
"reference" href=
"#introduction" id=
"id1" name=
"id1">Introduction
</a></li>
37 <li><a class=
"reference" href=
"#background-with-tongue-firmly-in-cheek" id=
"id2" name=
"id2">Background (with tongue firmly in cheek)
</a></li>
38 <li><a class=
"reference" href=
"#basic-program-example" id=
"id3" name=
"id3">Basic Program Example
</a></li>
39 <li><a class=
"reference" href=
"#hello-wxpython-example" id=
"id4" name=
"id4">Hello wxPython Example
</a></li>
42 <div class=
"section" id=
"introduction">
43 <h1><a class=
"toc-backref" href=
"#id1" name=
"introduction">Introduction
</a></h1>
44 <p>This document illustrates example programs using wxPython. All the
45 examples make use of the new wx package syntax introduced in wxPython
46 2.4.1, which is a bit different than older examples you might come
49 <div class=
"section" id=
"background-with-tongue-firmly-in-cheek">
50 <h1><a class=
"toc-backref" href=
"#id2" name=
"background-with-tongue-firmly-in-cheek">Background (with tongue firmly in cheek)
</a></h1>
51 <p>If something hits you on the head, don't run around screaming that the
52 sky is falling. Instead, take a close look and see if it wasn't a
53 "wx
" prefix that hit you. Apparently, they're dropping off wxPython
54 class names like flies dropping dead in the scorching heat of a
56 <p>Yes, the world is changing, and even our little wxPython world must
57 change with it. Then again, I'm not fond of pesky summertime flies,
58 and I'm not too upset that the
"wx
" prefixes are going to bite the
59 dust. I think it's for the best. But, being the kind, considerate
60 person that I am, I decided to write this guide to make the wx
61 namespace transition easier for everyone, even Chicken Little.
</p>
63 <p class=
"admonition-title">Note
</p>
65 <p>If you have no idea what I mean by the
"wx namespace transition,
"
66 consider yourself lucky. You can simply use these examples to
67 learn wxPython in its current state (beginning with wxPython
68 version
2.4.1). All you need to know is that previous wxPython
69 code used a slightly different syntax that some folks (including
70 me) considered ugly. So we changed it. And that's when the sky
71 starting falling...
</p>
72 <p>If you want more of the technical details, read the
<a class=
"reference" href=
"wxPackage.html">wx package
73 documentation
</a>.
</p>
75 <p>Rather than simply
<strong>tell
</strong> you that everything will be okay, I
76 decided to
<strong>show
</strong> you that everything will be okay. To do that,
77 I've created a bunch of example programs using the new wx package. I
78 hope you like them.
</p>
80 <div class=
"section" id=
"basic-program-example">
81 <h1><a class=
"toc-backref" href=
"#id3" name=
"basic-program-example">Basic Program Example
</a></h1>
82 <p>It doesn't get much simpler than this. Every wxPython program needs
83 an application and a frame. To encourage good coding habits, I've
84 split them into separate modules. They don't do much, but they're a
85 good starting point.
</p>
86 <p>I include a simple App class in the frame module because the PyWrap
87 "wrapper
" utility (
<tt class=
"literal"><span class=
"pre">pywrap
</span></tt>) only works with modules that contain an
88 application class. So including a simple one in each of your frame
89 modules allows you to use the PyWrap runtime wrapper and debug your
90 frames independent of your full application.
</p>
91 <p>Here is the module (
<tt class=
"literal"><span class=
"pre">frame.py
</span></tt>) that defines the frame class:
</p>
92 <pre class=
"literal-block">
95 """Basic frame class, with App for testing.
"""
97 __author__ =
"Patrick K. O'Brien
<pobrien
@orbtech.com
>"
98 __cvsid__ =
"$Id$
"
99 __revision__ =
"$Revision$
"[
11:-
2]
103 class Frame(wx.Frame):
104 """Frame class.
"""
106 def __init__(self, parent=None, id=-
1, title='Title',
107 pos=wx.DefaultPosition, size=(
400,
200)):
108 """Create a Frame instance.
"""
109 wx.Frame.__init__(self, parent, id, title, pos, size)
112 """Application class.
"""
117 self.SetTopWindow(self.frame)
124 if __name__ == '__main__':
128 <p>And here is the module (
<tt class=
"literal"><span class=
"pre">app.py
</span></tt>) that defines the application class
129 and imports the frame from
<tt class=
"literal"><span class=
"pre">frame.py
</span></tt>:
</p>
130 <pre class=
"literal-block">
131 #!/usr/bin/env python
133 """Basic application class.
"""
135 __author__ =
"Patrick K. O'Brien
<pobrien
@orbtech.com
>"
136 __cvsid__ =
"$Id$
"
137 __revision__ =
"$Revision$
"[
11:-
2]
141 from frame import Frame
144 """Application class.
"""
149 self.SetTopWindow(self.frame)
156 if __name__ == '__main__':
161 <div class=
"section" id=
"hello-wxpython-example">
162 <h1><a class=
"toc-backref" href=
"#id4" name=
"hello-wxpython-example">Hello wxPython Example
</a></h1>
163 <p>This program displays an image file (
<tt class=
"literal"><span class=
"pre">wxPython.jpg
</span></tt>) inside a frame
164 sized to match the graphic.
</p>
166 <p><img alt=
"screenshots/hello-win98.png" scale=
"100" src=
"screenshots/hello-win98.png" /></p>
167 <p class=
"caption">Running
<tt class=
"literal"><span class=
"pre">hello.py
</span></tt> on Windows.
</p>
170 <p><img alt=
"screenshots/hello-linux.png" scale=
"100" src=
"screenshots/hello-linux.png" /></p>
171 <p class=
"caption">Running
<tt class=
"literal"><span class=
"pre">hello.py
</span></tt> on Linux.
</p>
174 <p><img alt=
"screenshots/hello-mac.png" scale=
"100" src=
"screenshots/hello-mac.png" /></p>
175 <p class=
"caption">Running
<tt class=
"literal"><span class=
"pre">hello.py
</span></tt> on Mac OS X.
</p>
177 <p>Here is the source code for
<tt class=
"literal"><span class=
"pre">hello.py
</span></tt>:
</p>
178 <pre class=
"literal-block">
179 #!/usr/bin/env python
181 """Hello, wxPython! program.
"""
183 __author__ =
"Patrick K. O'Brien
<pobrien
@orbtech.com
>"
184 __cvsid__ =
"$Id$
"
185 __revision__ =
"$Revision$
"[
11:-
2]
189 class Frame(wx.Frame):
190 """Frame class that displays an image.
"""
192 def __init__(self, image, parent=None, id=-
1,
193 pos=wx.DefaultPosition, title='Hello, wxPython!'):
194 """Create a Frame instance and display image.
"""
195 temp = image.ConvertToBitmap()
196 size = temp.GetWidth(), temp.GetHeight()
197 wx.Frame.__init__(self, parent, id, title, pos, size)
198 self.bmp = wx.StaticBitmap(parent=self, id=-
1, bitmap=temp)
201 """Application class.
"""
204 wx.InitAllImageHandlers()
205 image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
206 self.frame = Frame(image)
208 self.SetTopWindow(self.frame)
215 if __name__ == '__main__':
221 <hr class=
"footer" />
223 Generated on:
2004-
02-
04 23:
31 UTC.