<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>learning python &#187; wxPython</title>
	<atom:link href="http://www.learningpython.com/category/python/wxpython/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningpython.com</link>
	<description>one man's journey into python...</description>
	<lastBuildDate>Mon, 26 Apr 2010 01:21:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a GUI using Python, WxWidgets, and wxPython</title>
		<link>http://www.learningpython.com/2007/01/29/creating-a-gui-using-python-wxwidgets-and-wxpython/</link>
		<comments>http://www.learningpython.com/2007/01/29/creating-a-gui-using-python-wxwidgets-and-wxpython/#comments</comments>
		<pubDate>Mon, 29 Jan 2007 14:25:53 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[gui]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=54</guid>
		<description><![CDATA[
			
				
			
		
After putting off this tutorial for as long as I possibly could (I&#8217;m not sure why) I have finally decided to buckle down and learn how to use WxWidgets using the WxPython Python bindings.
Installing
Getting WxWidgets and WxPython installed on my Debian Linux computer was as simple as installing the python-wxgtk2.6 Debian package.  If you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F01%2F29%2Fcreating-a-gui-using-python-wxwidgets-and-wxpython%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F01%2F29%2Fcreating-a-gui-using-python-wxwidgets-and-wxpython%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>After putting off this tutorial for as long as I possibly could (I&#8217;m not sure why) I have finally decided to buckle down and learn how to use <a href="http://www.wxwidgets.org/">WxWidgets</a> using the <a href="http://www.wxpython.org/">WxPython</a> Python bindings.</p>
<h2>Installing</h2>
<p>Getting WxWidgets and WxPython installed on my Debian Linux computer was as simple as installing the python-wxgtk2.6 Debian package.  If you have any problems installing wxPython on your computer I suggest that you follow the instructions in the wxPython <a href="http://wiki.wxpython.org/index.cgi/Frequently_Asked_Questions#head-d0fc93e98b44817371f049e77c983ab8eff716c8">FAQ</a>.  Or you can follow the Getting Started instructions on the <a href="http://wiki.wxpython.org/index.cgi/Getting_Started#head-76c76195679b0098d0d34e2d28e2e8c55a9e087f">wxPython wiki</a>.</p>
<h2>Setting up the Window</h2>
<p>Now that you have WxWidgets and WxPython installed it&#8217;s time to write a quick wxPython application.  Start up your favourite python editor and enter the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">wx

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">App</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">MainLoop</span><span class="hl-brackets">()</span></pre></div></div>
<p>Save the file as wxHello.py, or whatever you would like.  You can run the file if you want but you won&#8217;t actually see anything.  The first thing that you will notice is that we import wx, this will import wxPython into your program.</p>
<p>The next thing that we do is create a <a href="http://wxpython.wxcommunity.com/docs/api/wx.App-class.html">wx.App</a> and then call it&#8217;s MainLoop function, which starts the main GUI event loop.  Here is a description of the wx.App class from the wxPython documentation : </p>
<blockquote><p>The wx.App class represents the application and is used to:</p>
<ul>
<li>bootstrap the wxPython system and initialize the underlying gui toolkit</li>
<li>set and get application-wide properties</li>
<li>implement the windowing system main message or event loop, and to dispatch events to window instances</li>
<li>etc.</li>
</ul>
<p>Every application must have a wx.App instance, and all creation of UI objects should be delayed until after the wx.App object has been created in order to ensure that the gui platform and wxWidgets have been fully initialized.</p>
<p>Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).</p></blockquote>
<p>Notice the last little instruction there:</p>
<blockquote><p>Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).</p></blockquote>
<p>So that is our next step, derive a class from wx.App and override the OnInit member:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">wx

</span><span class="hl-reserved">class </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">App</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The wx.App for the wxHello application</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-reserved">def </span><span class="hl-identifier">OnInit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Override OnInit to create our Frame</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">frame</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Frame</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">wxHello</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">frame</span><span class="hl-default">.</span><span class="hl-identifier">Show</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetTopWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">frame</span><span class="hl-brackets">)
		</span><span class="hl-reserved">return True

if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">MainLoop</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now instead of creating a wx.App we create a wxHelloApp that is derived from a wx.App.  All that the wxHelloApp does is override the OnInit() function.  In that function it creates a <a href="http://wxpython.wxcommunity.com/docs/api/wx.Frame-class.html">wx.Frame</a> (from the wxWidget docs):</p>
<blockquote><p>A frame is a window whose size and position can (usually) be changed by the user. It usually has thick borders and a title bar, and can optionally contain a menu bar, toolbar and status bar. A frame can contain any window that is not a frame or dialog.</p></blockquote>
<p>When we create the frame we set its parent to be nothing, and it&#8217;s title to be &#8220;wxHello&#8221;.  If you run the wxHello.py file now you will be greeted by the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/wxHello_01/wxHello_01png" alt="Python wxPython" border="0" /></p>
<p><span id="more-54"></span><br />
It&#8217;s not much but it&#8217;s a start.  Now we are going to do the same thing to the wx.Frame class that we did to the wx.App class:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">wx

</span><span class="hl-reserved">class </span><span class="hl-identifier">wxHelloFrame</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Frame</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is the frame for our application, it is derived from
	the wx.Frame element.</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, *</span><span class="hl-identifier">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize, and let the user set any Frame settings</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Frame</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, *</span><span class="hl-identifier">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">create_controls</span><span class="hl-brackets">()

	</span><span class="hl-reserved">def </span><span class="hl-identifier">create_controls</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the controls on Window are to be created</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">pass

class </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">App</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The wx.App for the wxHello application</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-reserved">def </span><span class="hl-identifier">OnInit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Override OnInit to create our Frame</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">frame</span><span class="hl-default"> = </span><span class="hl-identifier">wxHelloFrame</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">wxHello</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">frame</span><span class="hl-default">.</span><span class="hl-identifier">Show</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetTopWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">frame</span><span class="hl-brackets">)

		</span><span class="hl-reserved">return True

if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">MainLoop</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now if you run this code the result will be identical to the previous run, the only difference is in the internal code.  Instead of creating a wx.Frame directly we create an instance of our wx.Frame derived class wxHelloFrame.  </p>
<p>wxHelloFrame does nothing except initialize its parent class and then call the create_controls() function which is where we will create our widgets in the future but for now does nothing.</p>
<h2>Adding the Widgets</h2>
<p>Now we are going to start adding the widgets to the frame in the create_controls() function.  Since we want our window to be resizeable (which they are in wxPython by default) we are going to use <a href="http://wxpython.wxcommunity.com/docs/api/wx.Sizer-class.html">wx.Sizer</a> objects so that our widgets also resize (from the WxPython docs):</p>
<blockquote><p>wx.Sizer is the abstract base class used for laying out subwindows in a window. You cannot use wx.Sizer directly; instead, you will have to use one of the sizer classes derived from it such as wx.BoxSizer, wx.StaticBoxSizer, wx.GridSizer, wx.FlexGridSizer and wx.GridBagSizer.</p>
<p>The concept implemented by sizers in wxWidgets is closely related to layout tools in other GUI toolkits, such as Java&#8217;s AWT, the GTK toolkit or the Qt toolkit. It is based upon the idea of the individual subwindows reporting their minimal required size and their ability to get stretched if the size of the parent window has changed. This will most often mean that the programmer does not set the original size of a dialog in the beginning, rather the dialog will assigned a sizer and this sizer will be queried about the recommended size. The sizer in turn will query its children, which can be normal windows or contorls, empty space or other sizers, so that a hierarchy of sizers can be constructed. Note that wxSizer does not derive from wxWindow and thus do not interfere with tab ordering and requires very little resources compared to a real window on screen.</p></blockquote>
<p>We are going to use <a href="http://wxpython.wxcommunity.com/docs/api/wx.BoxSizer-class.html">wx.BoxSizer</a> objects to lay out our widgets (from the wxPython docs):</p>
<blockquote><p>The basic idea behind a box sizer is that windows will most often be laid out in rather simple basic geometry, typically in a row or a column or nested hierarchies of either. A wx.BoxSizer will lay out its items in a simple row or column, depending on the orientation parameter passed to the constructor.</p>
<p>It is the unique feature of a box sizer, that it can grow in both directions (height and width) but can distribute its growth in the main direction (horizontal for a row) unevenly among its children. This is determined by the proportion parameter give to items when they are added to the sizer. It is interpreted as a weight factor, i.e. it can be zero, indicating that the window may not be resized at all, or above zero. If several windows have a value above zero, the value is interpreted relative to the sum of all weight factors of the sizer, so when adding two windows with a value of 1, they will both get resized equally and each will receive half of the available space after the fixed size items have been sized. If the items have unequal proportion settings then they will receive a coresondingly unequal allotment of the free space.</p></blockquote>
<p>Whew, that&#8217;s a lot of documentation, fortunately wx.Sizer&#8217;s aren&#8217;t that difficult to use.  So first we need to create a horizontal wx.BoxSizer (so that our widgets will resize horizontally) and then add <a href="http://wxpython.wxcommunity.com/docs/api/wx.StaticText-class.html">wx.StaticText</a> and <a href="http://wxpython.wxcommunity.com/docs/api/wx.TextCtrl-class.html">wx.TextCtrl</a> widgets to the sizer.  After that we simply need to set  the wx.boxSizer as the frame&#8217;s sizer and we are done.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_controls</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the controls on Window are to be created</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-comment"># Horizontal sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">HORIZONTAL</span><span class="hl-brackets">)

	</span><span class="hl-comment"># Create the static text widget and set the text
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">StaticText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Enter some text:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Create the Edit Field (or TextCtrl)
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">edit</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">TextCtrl</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-code">=</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Size</span><span class="hl-brackets">(</span><span class="hl-number">250</span><span class="hl-code">, -</span><span class="hl-number">1</span><span class="hl-brackets">))

	</span><span class="hl-comment">#Add to horizontal sizer
	#add the static text to the sizer, tell it not to resize
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Add 5 pixels between the static text and the edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">AddSpacer</span><span class="hl-brackets">((</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
	</span><span class="hl-comment">#Add Edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Set the sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">h_sizer</span><span class="hl-brackets">)</span></pre></div></div>
<p>So, as you can see creating the wx.BoxSize and the wx.StaticText widgets is pretty straightforward.  When we create the wx.TextCtrl widget you&#8217;ll notice that we are setting the default size of the widget using a <a href="http://wxpython.wxcommunity.com/docs/api/wx.Size-class.html">wx.Size</a> object.  We are setting the default width to be 250 and the default height to be -1.  When you use -1 as the value for a parameter in wxWidgets it generally means that that parameter should be ignored.  So since we don&#8217;t want to set the height of the edit field, we want wxWidgets to do that for us automatically, we set the height to be -1.</p>
<p>You&#8217;ll also notice that the first parameter when we created the wx.StaticText and wx.TextCtrl widgets is self, or our wxHelloFrame.  This means that the wxHelloFrame will be the parent window of the widgets.</p>
<p>We then add the two widgets to the wx.BoxSizer using the <a href="http://wxpython.wxcommunity.com/docs/api/wx.Sizer-class.html#Add">wx.Sizer.Add</a> function. When we add the wx.Statictext we set the resize proportion to be 0 since we don&#8217;t want it to resize, and when we add the wx.TextCtrl we set its resize proportion to be 1, since we want it to resize directly with the window as it resizes. </p>
<p>You&#8217;ll also notice that we add a spacer in in between the wx.StaticText and wx.TextCtrl widgets using the <a href="http://wxpython.wxcommunity.com/docs/api/wx.Sizer-class.html#AddSpacer">wx.Sizer.AddSpacer()</a> function.  We do this to add a 5 pixel horizontal buffer between the two widgets just so that things look nicer.</p>
<p>Finally we set the wxHelloFrame&#8217;s sizer to be the horizontal sizer and we are done.  If you run this code you will see the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/wxHello_01/wxHello_02.png" alt="Python wxPython" border="0" /></p>
<p>So that&#8217;s pretty neat, you have a window with some widgets that resize.  The next thing that we are going to do is add a <a href="http://wxpython.wxcommunity.com/docs/api/wx.Button-class.html">wx.Button</a> underneath the wx.StaticText widget.  </p>
<p>To do this we will need to use another wx.BoxSizer, this one being a vertical sizer.  Then we will add our horizontal wx.BoxSizer to the vertical sizer and then add the button.  This will place the contents of the horizontal box sizer over top of the button.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_controls</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the controls on Window are to be created</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-comment">#Horizontal sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">HORIZONTAL</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Vertical sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">VERTICAL</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Widget Creation
	#Create the static text widget and set the text
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">StaticText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Enter some text:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Create the Edit Field (or TextCtrl)
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">edit</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">TextCtrl</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-code">=</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Size</span><span class="hl-brackets">(</span><span class="hl-number">250</span><span class="hl-code">, -</span><span class="hl-number">1</span><span class="hl-brackets">))
	</span><span class="hl-comment">#Create the button
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Button</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Press me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Add to horizontal sizer
	#add the static text to the sizer, tell it not to resize
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Add 5 pixels between the static text and the edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">AddSpacer</span><span class="hl-brackets">((</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
	</span><span class="hl-comment">#Add Edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Add to the vertical sizer to create two rows
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">h_sizer</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EXPAND</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Add button underneath
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">button</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Set the sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-brackets">)</span></pre></div></div>
<p>So now we create both a horizontal and vertical wx.BoxSizer and a wx.Button widget with specific label text.  We then add the horizontal size to the vertical sizer, and then we add the button creating two rows of widgets.  We set the resize proportion of both the button and the horizontal sizer to be 0 since we don&#8217;t want them resizing vertically.  However we set the horizontal sizer&#8217;s flag to be wx.EXPAND which means:</p>
<blockquote><p>
The item will be expanded to fill the space allotted to the item.</p></blockquote>
<p>We do this so that the horizontal sizer will continue to resize horizontally.  Finally we set the sizer of the wxHelloFrame to be the vertical sizer.  If you run the code you will see the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/wxHello_01/wxHello_03.png" alt="Python wxPython" border="0" /></p>
<p>One thing that you will notice when you run the program is that the window starts off too big for the widgets and you can resize the window smaller then the size of the widgets.  This isn&#8217;t very nice so we need to add in some code to set the initial size of the window and to set the minimum size of the window.  </p>
<p>The other thing that we need to do is do something when the button is clicked.  We will make it so that when the button is clicked a message box comes up displaying whatever text was typed in the wx.TextCtrl.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_controls</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the controls on Window are to be created</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-comment">#Horizontal sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">HORIZONTAL</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Vertical sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">VERTICAL</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Widget Creation
	#Create the static text widget and set the text
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">StaticText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Enter some text:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Create the Edit Field (or TextCtrl)
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">edit</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">TextCtrl</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-code">=</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Size</span><span class="hl-brackets">(</span><span class="hl-number">250</span><span class="hl-code">, -</span><span class="hl-number">1</span><span class="hl-brackets">))
	</span><span class="hl-comment">#Create the button
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Button</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Press me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-comment">#bind the button click to our press function
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default">.</span><span class="hl-identifier">Bind</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EVT_BUTTON</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Add to horizontal sizer
	#add the static text to the sizer, tell it not to resize
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Add 5 pixels between the static text and the edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">AddSpacer</span><span class="hl-brackets">((</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
	</span><span class="hl-comment">#Add Edit
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Add to the vertical sizer to create two rows
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">h_sizer</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EXPAND</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Add button underneath
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">button</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Set the sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Fit ourselves to the sizer
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Fit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Set the Minumum size
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetMinSize</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-code">.</span><span class="hl-identifier">GetMinSize</span><span class="hl-brackets">())
	
</span><span class="hl-reserved">def </span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the button is clicked.  It will
	show a simple dialog with the text from the
	edit field.</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-comment">#Get the message text
	</span><span class="hl-identifier">message_text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">You typed: %s</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">.</span><span class="hl-identifier">GetValue</span><span class="hl-brackets">())
	</span><span class="hl-identifier">msg_box</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">MessageDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
		, </span><span class="hl-identifier">message_text</span><span class="hl-code">
		, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello!</span><span class="hl-quotes">&quot;</span><span class="hl-code">
		, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">OK</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">CENTRE</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">ICON_EXCLAMATION</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Show the Dialog
	</span><span class="hl-identifier">msg_box</span><span class="hl-default">.</span><span class="hl-identifier">ShowModal</span><span class="hl-brackets">()</span></pre></div></div>
<p>Binding the wx.Button to our function is pretty simple:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#bind the button click to our press function
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default">.</span><span class="hl-identifier">Bind</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EVT_BUTTON</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">)</span></pre></div></div>
<p>We simply call the <a href="http://wxpython.wxcommunity.com/docs/api/wx.EvtHandler-class.html#Bind">wx.Button.Bind()</a> function, bind it to the wx.EVT_BUTTON event, and tell it what function of ours to call when the button is pressed.</p>
<p>Then to initially size the window and set its minimum size we do the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Fit ourselves to the sizer
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Fit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)
</span><span class="hl-comment">#Set the Minumum size
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetMinSize</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-code">.</span><span class="hl-identifier">GetMinSize</span><span class="hl-brackets">())</span></pre></div></div>
<p>To size the frame to the sizer we call the <a href="http://wxpython.wxcommunity.com/docs/api/wx.Sizer-class.html#Fit">wx.Sizer.Fit()</a> function.  We then set the minimum size of the frame using the <a href="http://wxpython.wxcommunity.com/docs/api/wx.Window-class.html#SetMinSize">wx.Frame.SetMinSize()</a> function to the minimum size of the vertical sizer which we get using the <a href="http://www.learningpython.com/wp-admin/post.php?action=edit&#038;post=54">ws.Sizer.GetWinSize()</a> function.</p>
<p>Next we handle the button&#8217;s press event using the following function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the button is clicked.  It will
	show a simple dialog with the text from the
	edit field.</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-comment">#Get the message text
	</span><span class="hl-identifier">message_text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">You typed: %s</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">.</span><span class="hl-identifier">GetValue</span><span class="hl-brackets">())
	</span><span class="hl-identifier">msg_box</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">MessageDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
		, </span><span class="hl-identifier">message_text</span><span class="hl-code">
		, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello!</span><span class="hl-quotes">&quot;</span><span class="hl-code">
		, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">OK</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">CENTRE</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">ICON_EXCLAMATION</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Show the Dialog
	</span><span class="hl-identifier">msg_box</span><span class="hl-default">.</span><span class="hl-identifier">ShowModal</span><span class="hl-brackets">()</span></pre></div></div>
<p>This function is pretty straight forward, first we get the text from the wx.TextCtrl widget using the wx.TextCtrl.GetValue() function.  Then we create  a <a href="http://wxpython.wxcommunity.com/docs/api/wx.MessageDialog-class.html">wx.MessageDialog</a> instance.  The wx.MessageDialog is &#8220;a simple dialog that shows a single or multi-line message, with a choice of OK, Yes, No and/or Cancel buttons.&#8221;  We set the parent, title, text of the dialog and use the following flags to control how it looks and functions:</p>
<ul>
<li>wx.OK &#8211; Show an OK button.</li>
<li>wx.ICON_EXCLAMATION &#8211; Shows an exclamation mark icon.</li>
<li>wx.CENTER &#8211; Is supposed to center the dialog on it&#8217;s parent.</li>
</ul>
<p>Then we show the dialog using the wx.Dialog.ShowModal() function.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/wxHello_01/wxHello_04.png" alt="Python wxPython" border="0" /></p>
<p>Here is the all the code:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">wx

</span><span class="hl-reserved">class </span><span class="hl-identifier">wxHelloFrame</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Frame</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is the frame for our application, it is derived from
	the wx.Frame element.</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, *</span><span class="hl-identifier">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize, and let the user set any Frame settings</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Frame</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, *</span><span class="hl-identifier">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">create_controls</span><span class="hl-brackets">()

	</span><span class="hl-reserved">def </span><span class="hl-identifier">create_controls</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the controls on Window are to be created</span><span class="hl-quotes">&quot;&quot;&quot;

		</span><span class="hl-comment">#Horizontal sizer
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">HORIZONTAL</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Vertical sizer
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">BoxSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">VERTICAL</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Widget Creation
		#Create the static text widget and set the text
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">StaticText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Enter some text:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Create the Edit Field (or TextCtrl)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">edit</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">TextCtrl</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-code">=</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">Size</span><span class="hl-brackets">(</span><span class="hl-number">250</span><span class="hl-code">, -</span><span class="hl-number">1</span><span class="hl-brackets">))
		</span><span class="hl-comment">#Create the button
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">Button</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Press me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-comment">#bind the button click to our press function
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">button</span><span class="hl-default">.</span><span class="hl-identifier">Bind</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EVT_BUTTON</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Add to horizontal sizer
		#add the static text to the sizer, tell it not to resize
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Add 5 pixels between the static text and the edit
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">AddSpacer</span><span class="hl-brackets">((</span><span class="hl-number">5</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
		</span><span class="hl-comment">#Add Edit
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">h_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Add to the vertical sizer to create two rows
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">h_sizer</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">EXPAND</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Add button underneath
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">button</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Set the sizer
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetSizer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Fit ourselves to the sizer
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">v_sizer</span><span class="hl-default">.</span><span class="hl-identifier">Fit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Set the Minumum size
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetMinSize</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">v_sizer</span><span class="hl-code">.</span><span class="hl-identifier">GetMinSize</span><span class="hl-brackets">())

	</span><span class="hl-reserved">def </span><span class="hl-identifier">on_button_pressed</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the button is clicked.  It will
		show a simple dialog with the text from the
		edit feld.</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-comment">#Get the message text
		</span><span class="hl-identifier">message_text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">You typed: %s</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">edit</span><span class="hl-code">.</span><span class="hl-identifier">GetValue</span><span class="hl-brackets">())
		</span><span class="hl-identifier">msg_box</span><span class="hl-default"> = </span><span class="hl-identifier">wx</span><span class="hl-default">.</span><span class="hl-identifier">MessageDialog</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
			, </span><span class="hl-identifier">message_text</span><span class="hl-code">
			, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello!</span><span class="hl-quotes">&quot;</span><span class="hl-code">
			, </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">OK</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">CENTRE</span><span class="hl-code"> | </span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">ICON_EXCLAMATION</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Show the Dialog
		</span><span class="hl-identifier">msg_box</span><span class="hl-default">.</span><span class="hl-identifier">ShowModal</span><span class="hl-brackets">()

</span><span class="hl-reserved">class </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">(</span><span class="hl-identifier">wx</span><span class="hl-code">.</span><span class="hl-identifier">App</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The wx.App for the wxHello application</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-reserved">def </span><span class="hl-identifier">OnInit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Override OnInit to create our Frame</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">frame</span><span class="hl-default"> = </span><span class="hl-identifier">wxHelloFrame</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-identifier">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">wxHello</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">frame</span><span class="hl-default">.</span><span class="hl-identifier">Show</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SetTopWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">frame</span><span class="hl-brackets">)

		</span><span class="hl-reserved">return True

if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">wxHelloApp</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">MainLoop</span><span class="hl-brackets">()</span></pre></div></div>
<h2>Wrap up</h2>
<p>So that&#8217;s it for this simple wxWidgets tutorial I hope everything makes sense.  All-in-all I found my first experience with wxPython to be quite enjoyable, I would have liked the wxPython documentation to be a bit more robust but using it and some of the other examples on the Internet was enough for me to muddle my way through.</p>
<p>I still think that I prefer pyGTK to wxPython but the fact that I have more experience with pyGTK probably plays a large role in that.  I also like that the fact the pyGTk seems to take a different approach to the other GUI tool kits that I have used making it able to do some really neat things.  wxWidgets on the other hand seems to be more similar to other toolkits, which made me instantly more familiar with some of its functionality.</p>
<p>As always if you have any questions or comments, or if you notice any bugs or style irregularities feel free to add a comment below. </p>
<p>If you have any of your own wxPython tips or want to ask some wxPython questions why don&#8217;t you do so in the <a href="http://www.learningpython.com/forums/">learningPython fourms</a>?</p>
<p>Now it&#8217;s time for a glass of wine!</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2007/01/29/creating-a-gui-using-python-wxwidgets-and-wxpython/&title=Creating a GUI using Python, WxWidgets, and wxPython&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2007/01/29/creating-a-gui-using-python-wxwidgets-and-wxpython/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
