]> git.saurik.com Git - wxWidgets.git/blob - wxPython/docs/wxPythonExamples.html
Script updates
[wxWidgets.git] / wxPython / docs / wxPythonExamples.html
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">
4 <head>
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" />
12 </head>
13 <body>
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" />
20 <tbody valign="top">
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&#64;orbtech.com">pobrien&#64;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>
30 <td>1.2</td></tr>
31 </tbody>
32 </table>
33 <div class="contents topic" id="contents">
34 <p class="topic-title"><a name="contents">Contents</a></p>
35 <ul class="simple">
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>
40 </ul>
41 </div>
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
47 across.</p>
48 </div>
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 &quot;wx&quot; prefix that hit you. Apparently, they're dropping off wxPython
54 class names like flies dropping dead in the scorching heat of a
55 summer's day.</p>
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 &quot;wx&quot; 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>
62 <div class="note">
63 <p class="admonition-title">Note</p>
64 <p>Say what?</p>
65 <p>If you have no idea what I mean by the &quot;wx namespace transition,&quot;
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>
74 </div>
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>
79 </div>
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 &quot;wrapper&quot; 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">
93 #!/usr/bin/env python
94
95 &quot;&quot;&quot;Basic frame class, with App for testing.&quot;&quot;&quot;
96
97 __author__ = &quot;Patrick K. O'Brien &lt;pobrien&#64;orbtech.com&gt;&quot;
98 __cvsid__ = &quot;$Id$&quot;
99 __revision__ = &quot;$Revision$&quot;[11:-2]
100
101 import wx
102
103 class Frame(wx.Frame):
104 &quot;&quot;&quot;Frame class.&quot;&quot;&quot;
105
106 def __init__(self, parent=None, id=-1, title='Title',
107 pos=wx.DefaultPosition, size=(400, 200)):
108 &quot;&quot;&quot;Create a Frame instance.&quot;&quot;&quot;
109 wx.Frame.__init__(self, parent, id, title, pos, size)
110
111 class App(wx.App):
112 &quot;&quot;&quot;Application class.&quot;&quot;&quot;
113
114 def OnInit(self):
115 self.frame = Frame()
116 self.frame.Show()
117 self.SetTopWindow(self.frame)
118 return True
119
120 def main():
121 app = App()
122 app.MainLoop()
123
124 if __name__ == '__main__':
125 main()
126
127 </pre>
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
132
133 &quot;&quot;&quot;Basic application class.&quot;&quot;&quot;
134
135 __author__ = &quot;Patrick K. O'Brien &lt;pobrien&#64;orbtech.com&gt;&quot;
136 __cvsid__ = &quot;$Id$&quot;
137 __revision__ = &quot;$Revision$&quot;[11:-2]
138
139 import wx
140
141 from frame import Frame
142
143 class App(wx.App):
144 &quot;&quot;&quot;Application class.&quot;&quot;&quot;
145
146 def OnInit(self):
147 self.frame = Frame()
148 self.frame.Show()
149 self.SetTopWindow(self.frame)
150 return True
151
152 def main():
153 app = App()
154 app.MainLoop()
155
156 if __name__ == '__main__':
157 main()
158
159 </pre>
160 </div>
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>
165 <div class="figure">
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>
168 </div>
169 <div class="figure">
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>
172 </div>
173 <div class="figure">
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>
176 </div>
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
180
181 &quot;&quot;&quot;Hello, wxPython! program.&quot;&quot;&quot;
182
183 __author__ = &quot;Patrick K. O'Brien &lt;pobrien&#64;orbtech.com&gt;&quot;
184 __cvsid__ = &quot;$Id$&quot;
185 __revision__ = &quot;$Revision$&quot;[11:-2]
186
187 import wx
188
189 class Frame(wx.Frame):
190 &quot;&quot;&quot;Frame class that displays an image.&quot;&quot;&quot;
191
192 def __init__(self, image, parent=None, id=-1,
193 pos=wx.DefaultPosition, title='Hello, wxPython!'):
194 &quot;&quot;&quot;Create a Frame instance and display image.&quot;&quot;&quot;
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)
199
200 class App(wx.App):
201 &quot;&quot;&quot;Application class.&quot;&quot;&quot;
202
203 def OnInit(self):
204 wx.InitAllImageHandlers()
205 image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
206 self.frame = Frame(image)
207 self.frame.Show()
208 self.SetTopWindow(self.frame)
209 return True
210
211 def main():
212 app = App()
213 app.MainLoop()
214
215 if __name__ == '__main__':
216 main()
217
218 </pre>
219 </div>
220 </div>
221 <hr class="footer" />
222 <div class="footer">
223 Generated on: 2004-02-04 23:31 UTC.
224 </div>
225 </body>
226 </html>