]> git.saurik.com Git - wxWidgets.git/blame - wxPython/docs/wxPythonExamples.html
mention exit code change
[wxWidgets.git] / wxPython / docs / wxPythonExamples.html
CommitLineData
8eda5e35
RD
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
45examples make use of the new wx package syntax introduced in wxPython
462.4.1, which is a bit different than older examples you might come
47across.</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
52sky 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
54class names like flies dropping dead in the scorching heat of a
55summer's day.</p>
56<p>Yes, the world is changing, and even our little wxPython world must
57change with it. Then again, I'm not fond of pesky summertime flies,
58and I'm not too upset that the &quot;wx&quot; prefixes are going to bite the
59dust. I think it's for the best. But, being the kind, considerate
60person that I am, I decided to write this guide to make the wx
61namespace 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;
66consider yourself lucky. You can simply use these examples to
67learn wxPython in its current state (beginning with wxPython
68version 2.4.1). All you need to know is that previous wxPython
69code used a slightly different syntax that some folks (including
70me) considered ugly. So we changed it. And that's when the sky
71starting falling...</p>
72<p>If you want more of the technical details, read the <a class="reference" href="wxPackage.html">wx package
73documentation</a>.</p>
74</div>
75<p>Rather than simply <strong>tell</strong> you that everything will be okay, I
76decided to <strong>show</strong> you that everything will be okay. To do that,
77I've created a bunch of example programs using the new wx package. I
78hope 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
83an application and a frame. To encourage good coding habits, I've
84split them into separate modules. They don't do much, but they're a
85good 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
88application class. So including a simple one in each of your frame
89modules allows you to use the PyWrap runtime wrapper and debug your
90frames 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
101import wx
102
103class 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
111class 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
120def main():
121 app = App()
122 app.MainLoop()
123
124if __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
129and 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
139import wx
140
141from frame import Frame
142
143class 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
152def main():
153 app = App()
154 app.MainLoop()
155
156if __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
164sized 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
187import wx
188
189class 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
200class 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
211def main():
212 app = App()
213 app.MainLoop()
214
215if __name__ == '__main__':
216 main()
217
218</pre>
219</div>
220</div>
221<hr class="footer" />
222<div class="footer">
9c329f86 223Generated on: 2004-02-27 00:27 UTC.
8eda5e35
RD
224</div>
225</body>
226</html>