<?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>The Mouse Vs. The Python &#187; Tutorial</title>
	<atom:link href="http://www.blog.pythonlibrary.org/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Sun, 08 Jan 2012 12:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Shortening URLS with wxPython</title>
		<link>http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/</link>
		<comments>http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 23:48:40 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=400</guid>
		<description><![CDATA[During the summer, I stumbled upon an article on Ars Technica about using PyGTK to create a URL shortener. I thought that was pretty interesting, but I don&#8217;t use PyGTK. So at that point, I decided to write my own using wxPython and use the article&#8217;s code to do the shortening. I hacked together something [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/" data-url="http://bit.ly/t3pDkK" data-text="Shortening URLS with wxPython" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/";
			reddit_title = "Shortening URLS with wxPython";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/"></g:plusone></div></div><p>During the summer, I stumbled upon an <a href="http://arstechnica.com/open-source/guides/2009/07/howto-building-textbox-url-shortener-with-pygtk.ars">article</a> on Ars Technica about using PyGTK to create a URL shortener. I thought that was pretty interesting, but I don&#8217;t use PyGTK. So at that point, I decided to write my own using wxPython and use the article&#8217;s code to do the shortening. I hacked together something pretty quickly and then put this article on the back burner and kind of forgot about it. Today, I decided to go ahead and finish it and also create an application that can shorten URLs using other popular URL shorteners.<span id="more-400"></span></p>
<h2>Aping Ars</h2>
<p>First off, I&#8217;ll show you my original program that basically just apes the Ars Technica one.</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/wxArsShortener.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/wxArsShortener.png" alt="" title="wxArsShortener" width="300" height="70" class="aligncenter size-full wp-image-402" /></a></p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ArsShortener<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>,
                          <span style="color: #483d8b;">'wxArsShortner'</span>, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span>,<span style="color: #ff4500;">70</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add a panel so it looks the correct on all platforms</span>
        panel = wx.<span style="color: black;">Panel</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">txt</span> = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span>panel, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">&quot;&quot;</span>, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span>, <span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">txt</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_TEXT</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onTextChange</span><span style="color: black;">&#41;</span>
&nbsp;
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">txt</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        panel.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>sizer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onTextChange<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        text = <span style="color: #008000;">self</span>.<span style="color: black;">txt</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        textLength = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^https?://[^ ]+&quot;</span>, text<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> textLength <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">20</span>:
            apiurl = <span style="color: #483d8b;">&quot;http://is.gd/api.php?&quot;</span> + <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>longurl=text<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            shorturl = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>apiurl<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">txt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>shorturl<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    app = wx.<span style="color: black;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame = ArsShortener<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>As you can see, almost half the code is wxPython related while the other half is taken from the Ars article. To get started, we import the Python regular expressions module, re, for validation purposes. It checks the pasted URL to make sure it is in the right format for a URL and not just junk (see the onTextChange method). The urllib modules are used for accessing the <a href="http://is.gd">is.gd</a> website and passing it the URL that we want shortened. Once we get the result back, we call the text control&#8217;s SetValue method to display it to the user. The conditional statement also checks to see if the length of the URL is long enough to warrant shortening it.</p>
<p>You will notice that I have three lines commented out. I was making it so that the application could be resized and have the text control grow along with the frame, but then thought that might be lame. Thus, I commented it out. However, I thought you, dear reader, might find it useful to know how to accomplish this.</p>
<h2>Shortening URLs with Other Websites</h2>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/urlShortener.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/urlShortener-300x101.png" alt="" title="urlShortener" width="300" height="101" class="aligncenter size-medium wp-image-401" /></a></p>
<p>Now we will look at my expanded example. It can use is.gd, bit.ly or the venerable tinyurl to shorten urls. For this part of the article, you will need the following modules in addition to wx:</p>
<ul>
<li><a href="http://opie4624.github.com/bitly.py/">Opie&#8217;s bitly.py</a></li>
<li><a href="http://pypi.python.org/pypi/TinyUrl/0.1.0">TinyUrl 0.1.0+</a></li>
</ul>
<p>I&#8217;m sure there are ways to just use the urllib modules for these websites too, but I wanted to see if these third party modules simplified things. Please note that if you want to use bit.ly, they require you to sign up for an API key. I won&#8217;t be publishing mine, but I will show you how to use it in my program. So, without further ado, on with the show:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
bitlyFlag = <span style="color: #008000;">True</span>
tinyurlFlag = <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> bitly
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
    bitlyFlag = <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> tinyurl
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
    tinyurlFlag = <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MainPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent=parent, <span style="color: #008000;">id</span>=wx.<span style="color: black;">ID_ANY</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">frame</span> = parent
&nbsp;
        <span style="color: #808080; font-style: italic;"># create the widgets</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">createLayout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> createLayout<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Create widgets and lay them out
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        choices = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;is.gd&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> bitlyFlag:
            choices.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bit.ly&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> tinyurlFlag:
            choices.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tinyurl&quot;</span><span style="color: black;">&#41;</span>
        choices.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create the widgets</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">urlCbo</span> = wx.<span style="color: black;">ComboBox</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">&quot;is.gd&quot;</span>,
                                  choices=choices,
                                  size=wx.<span style="color: black;">DefaultSize</span>,
                                  style=wx.<span style="color: black;">CB_DROPDOWN</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">inputUrlTxt</span> = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value=<span style="color: #483d8b;">&quot;Paste long url here&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">inputUrlTxt</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_SET_FOCUS</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onFocus</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span> = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, style=wx.<span style="color: black;">TE_READONLY</span><span style="color: black;">&#41;</span>
&nbsp;
        shortenBtn = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Shorten URL&quot;</span><span style="color: black;">&#41;</span>
        shortenBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onShorten</span><span style="color: black;">&#41;</span>
        copyBtn = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Copy to Clipboard&quot;</span><span style="color: black;">&#41;</span>
        copyBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onCopy</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create the sizers</span>
        mainSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        btnSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout the widgets</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">urlCbo</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">inputUrlTxt</span>, <span style="color: #ff4500;">0</span>,
                      wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>, <span style="color: #ff4500;">0</span>,
                      wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        btnSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>shortenBtn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        btnSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>copyBtn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        mainSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>btnSizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>mainSizer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onCopy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Copies data to the clipboard or displays an error
        dialog if the clipboard is inaccessible.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        text = <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">do</span> = wx.<span style="color: black;">TextDataObject</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">do</span>.<span style="color: black;">SetText</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">Open</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">SetData</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">do</span><span style="color: black;">&#41;</span>
            wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            status = <span style="color: #483d8b;">&quot;Copied %s to clipboard&quot;</span> <span style="color: #66cc66;">%</span> text
            <span style="color: #008000;">self</span>.<span style="color: black;">frame</span>.<span style="color: black;">statusbar</span>.<span style="color: black;">SetStatusText</span><span style="color: black;">&#40;</span>status<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            wx.<span style="color: black;">MessageBox</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Unable to open the clipboard&quot;</span>, <span style="color: #483d8b;">&quot;Error&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onFocus<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        When control is given the focus, it is cleared
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">inputUrlTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onShorten<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Shortens a url using the service specified.
        Then sets the text control to the new url.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        text = <span style="color: #008000;">self</span>.<span style="color: black;">inputUrlTxt</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        textLength = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^https?://[^ ]+&quot;</span>, text<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> textLength <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">20</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            wx.<span style="color: black;">MessageBox</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;URL is already tiny!&quot;</span>, <span style="color: #483d8b;">&quot;Error&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
        url = <span style="color: #008000;">self</span>.<span style="color: black;">urlCbo</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> url == <span style="color: #483d8b;">&quot;is.gd&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">shortenWithIsGd</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url == <span style="color: #483d8b;">&quot;bit.ly&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">shortenWithBitly</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url == <span style="color: #483d8b;">&quot;tinyurl&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">shortenWithTinyurl</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> shortenWithBitly<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Shortens the URL in the text control using bit.ly
&nbsp;
        Requires a bit.ly account and API key
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        bitly.<span style="color: black;">API_LOGIN</span> = <span style="color: #483d8b;">&quot;username&quot;</span>
        bitly.<span style="color: black;">API_KEY</span> = <span style="color: #483d8b;">&quot;api_key&quot;</span>
        url = bitly.<span style="color: black;">shorten</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> shortenWithIsGd<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Shortens the URL with is.gd using urllib and urllib2
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
        apiurl = <span style="color: #483d8b;">&quot;http://is.gd/api.php?&quot;</span> + <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>longurl=text<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        shorturl = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>apiurl<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>shorturl<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> shortenWithTinyurl<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Shortens the URL with tinyurl
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;in tinyurl&quot;</span>
        url = tinyurl.<span style="color: black;">create_one</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> UrlFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    wx.Frame class
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        title = <span style="color: #483d8b;">&quot;URL Shortener&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>,
                          title=title, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">650</span>, <span style="color: #ff4500;">220</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        panel = MainPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">statusbar</span> = <span style="color: #008000;">self</span>.<span style="color: black;">CreateStatusBar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetMinSize</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">650</span>, <span style="color: #ff4500;">220</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame = UrlFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This piece of code is quite a bit longer than my simple example, but it has a lot more logic built into it. Right off the bat, I have some exception handling implemented in case the programmer doesn&#8217;t have the bitly or the tinyurl modules installed. If they do not, then a flag is set that prevents those options from being added. You&#8217;ll see this in action in the MainPanel class&#8217;s <em>createLayout</em> method. That is where we add the options to the <em>choices</em> list which our combobox will use. Depending on what you have installed, you will see one to three options in the drop-down list.</p>
<p>The next interesting bit is where the input url text control is bound to a focus event. We use this to clear the text control when we paste a url into it. Also take note that the output text control is set to read-only mode. This prevents the user from messing up the new url. Finally, we reach our last two widgets: the <em>Shorten URL</em> and the <em>Copy to Clipboard</em> buttons.</p>
<p>Let&#8217;s take a quick look at what happens in the onCopy method since it&#8217;s next:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> onCopy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Copies data to the clipboard or displays an error
    dialog if the clipboard is inaccessible.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    text = <span style="color: #008000;">self</span>.<span style="color: black;">outputUrlTxt</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">do</span> = wx.<span style="color: black;">TextDataObject</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">do</span>.<span style="color: black;">SetText</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">Open</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">SetData</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">do</span><span style="color: black;">&#41;</span>
        wx.<span style="color: black;">TheClipboard</span>.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        status = <span style="color: #483d8b;">&quot;Copied %s to clipboard&quot;</span> <span style="color: #66cc66;">%</span> text
        <span style="color: #008000;">self</span>.<span style="color: black;">frame</span>.<span style="color: black;">statusbar</span>.<span style="color: black;">SetStatusText</span><span style="color: black;">&#40;</span>status<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        wx.<span style="color: black;">MessageBox</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Unable to open the clipboard&quot;</span>, <span style="color: #483d8b;">&quot;Error&quot;</span><span style="color: black;">&#41;</span></pre>
<p>As you can see, this grabs the current text from the input text control and creates a TextDataObject out of it. Then we attempt to open the clipboard and if we&#8217;re successful, we put the TextDataObject into it using the clipboard&#8217;s SetData method. Finally, we alert the user to what we have done by changing the frame&#8217;s statusbar text.</p>
<p>In the onShorten method, I re-use the regular expression from the Ars program to check if the user has pasted a valid url and we also check the length to see if the url really needs shortening. We get the shortener url type from the combobox and then use a conditional that passes the url we want shortened to the appropriate shortening method. The <em>shortenWithIsGd</em> method is basically the same as the first example, so we&#8217;ll skip that one. The <em>shortenWithBitly</em> method shows that we need to set the LOGIN and API_KEY properties before we can shorten the url. Once we&#8217;ve done that, we just call bitly&#8217;s <em>shorten</em> method. In the <em>shortenWithTinyurl</em> method, it&#8217;s even simpler: all you need to do here is call the tinyurl&#8217;s <em>create_one</em> method. </p>
<h2>Wrapping Up</h2>
<p>Now you know the basics for shortening your long URLs via several methods. Feel free to add your own features or other shortening APIs to improve the application for your own use. Note that there&#8217;s another Python <a href="http://code.google.com/p/python-bitly/">bitly </a>module. While its home page says that all it needs is the simplejson module, if you actually try to use this module, you&#8217;ll receive an error if you don&#8217;t also have <a href="http://www.djangoproject.com/">django</a> installed. They seem to be aware of the issue, but for this reason, I chose to use Opie&#8217;s bitly module instead. Have fun coding!</p>
<p><em>Note: This code was tested on Windows Vista using wxPython 2.8.10.1 (unicode) with Python 2.5.</em></p>
<h2>Downloads</h2>
<ul>
<li><a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/shortening.tar'>shortening.tar</a></li>
<li><a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/shortening.zip'>shortening.zip</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2009/12/24/shortening-urls-with-wxpython/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>wxPython Sizers Tutorial: Using a GridBagSizer</title>
		<link>http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/</link>
		<comments>http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/#comments</comments>
		<pubDate>Fri, 23 May 2008 00:08:10 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[sizer]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=31</guid>
		<description><![CDATA[In this tutorial, I will take my code from the GridSizer tutorial I wrote the other day and heavily modify it to display oddly shaped widgets in a GridBagSizer. The GridBagSizer is the most complex of the sizers. It subclasses the FlexGridSizer, so you can use all of it&#8217;s parent&#8217;s methods as well as those [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/" data-url="http://bit.ly/sSuNSH" data-text="wxPython Sizers Tutorial: Using a GridBagSizer" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/";
			reddit_title = "wxPython Sizers Tutorial: Using a GridBagSizer";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/"></g:plusone></div></div><p>In this tutorial, I will take my code from the <a href="http://www.blog.pythonlibrary.org/?p=25">GridSizer tutorial</a> I wrote the other day and heavily modify it to display oddly shaped widgets in a GridBagSizer. The GridBagSizer is the most complex of the sizers. It subclasses the FlexGridSizer, so you can use all of it&#8217;s parent&#8217;s methods as well as those that the GridBagSizer adds. See <a href="http://wxpython.org/docs/api/wx.GridBagSizer-class.html">the docs</a> or use Python&#8217;s help() functionality for more information on said methods.</p>
<p><span id="more-31"></span></p>
<p>Here&#8217;s how you instantiate the GridBagSizer:</p>
<pre class="python">bagSizer    = wx.<span style="color: black;">GridBagSizer</span><span style="color: black;">&#40;</span>hgap=<span style="color: #ff4500;">5</span>, vgap=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span></pre>
<p>You&#8217;ll notice that you do not need to specify rows or columns when you create an instance of the GridBagSizer. When you get to the Add() function, you&#8217;ll notice that it adds the span parameter. This parameter allows you to specify if the widget can specify multiple rows, columns or both. Before I actually start doing that though, I&#8217;ll go over the odd-shaped widgets that we&#8217;ll be adding to the rows. </p>
<p>The first one I&#8217;m going to add is a RadioBox. First, you need to create a list of values for the RadioBox. Here&#8217;s the setup for this interesting widget:</p>
<pre class="python">rb = wx.<span style="color: black;">RadioBox</span><span style="color: black;">&#40;</span>parent, <span style="color: #008000;">id</span>, label, pos, size, choices, majorDimension,
                        style, validator, name<span style="color: black;">&#41;</span></pre>
<p>There&#8217;s only two parameters here that I think need additional explanation: choices and majorDimension. The &#8220;choices&#8221; parameter refers to the list of values you create. The majorDimension parameter is used to state the size of one dimension, either rows or columns, which is based on the style flag you use (either wx.RA_SPECIFY_COLS or wx.RA_SPECIFY_ROWS). It defaults to columns. If you want the opposite effect, then use the wx.RA_SPECIFY_ROWS flag. See the &#8220;wxPython in Action&#8221; book, page 215 for more information.</p>
<p>The other widget I&#8217;ll use is a multiline TextCtrl. All you need to do to make a TextCtrl go multiline is add  the style flag, wx.TE_MULTILINE. Once we have these added and the proper flags are in place, you should see something like this:</p>
<p><a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2008/05/funky_size.jpg'><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2008/05/funky_size-300x141.jpg" alt="" width="300" height="141" class="alignnone size-medium wp-image-32" /></a></p>
<p>Ok. Let&#8217;s look at the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyForm<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">'My Form'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add a panel so it looks the correct on all platforms</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">panel</span> = wx.<span style="color: black;">Panel</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span><span style="color: black;">&#41;</span>
&nbsp;
        bmp = wx.<span style="color: black;">ArtProvider</span>.<span style="color: black;">GetBitmap</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">ART_INFORMATION</span>, wx.<span style="color: black;">ART_OTHER</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
&nbsp;
        titleIco = wx.<span style="color: black;">StaticBitmap</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, bmp<span style="color: black;">&#41;</span>
        title = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">'My Title'</span><span style="color: black;">&#41;</span>
        title.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># 1st row of widgets</span>
        bmp = wx.<span style="color: black;">ArtProvider</span>.<span style="color: black;">GetBitmap</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">ART_TIP</span>, wx.<span style="color: black;">ART_OTHER</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        inputOneIco = wx.<span style="color: black;">StaticBitmap</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, bmp<span style="color: black;">&#41;</span>
        labelOne = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">'Name'</span><span style="color: black;">&#41;</span>
        inputTxtOne = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
&nbsp;
        sampleList = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'zero'</span>, <span style="color: #483d8b;">'one'</span>, <span style="color: #483d8b;">'two'</span>, <span style="color: #483d8b;">'three'</span>, <span style="color: #483d8b;">'four'</span>, <span style="color: #483d8b;">'five'</span>,
                      <span style="color: #483d8b;">'six'</span>, <span style="color: #483d8b;">'seven'</span>, <span style="color: #483d8b;">'eight'</span><span style="color: black;">&#93;</span>
        rb = wx.<span style="color: black;">RadioBox</span><span style="color: black;">&#40;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">&quot;wx.RadioBox&quot;</span>, wx.<span style="color: black;">DefaultPosition</span>,
                wx.<span style="color: black;">DefaultSize</span>, sampleList, <span style="color: #ff4500;">2</span>, wx.<span style="color: black;">RA_SPECIFY_COLS</span>
                <span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># 2nd row of widgets</span>
        multiTxt = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">''</span>,
                               size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">200</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>,
                               style=wx.<span style="color: black;">TE_MULTILINE</span><span style="color: black;">&#41;</span>
        sampleList = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'one'</span>, <span style="color: #483d8b;">'two'</span>, <span style="color: #483d8b;">'three'</span>, <span style="color: #483d8b;">'four'</span><span style="color: black;">&#93;</span>
        combo = wx.<span style="color: black;">ComboBox</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">'Default'</span>, wx.<span style="color: black;">DefaultPosition</span>,
                            <span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>, sampleList, wx.<span style="color: black;">CB_DROPDOWN</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Create the sizers</span>
        topSizer    = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        titleSizer  = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        bagSizer    = wx.<span style="color: black;">GridBagSizer</span><span style="color: black;">&#40;</span>hgap=<span style="color: #ff4500;">5</span>, vgap=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add widgets to sizers</span>
        titleSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>titleIco, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        titleSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>title, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>inputOneIco, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>,
                     flag=wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">ALIGN_CENTER_VERTICAL</span>,
                     border=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>labelOne, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,
                     flag=wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">ALIGN_CENTER_VERTICAL</span>,
                     border=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>inputTxtOne, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,
                     flag=wx.<span style="color: black;">EXPAND</span>|wx.<span style="color: black;">ALL</span>,
                     border=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
        bagSizer.<span style="color: black;">AddGrowableCol</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>rb, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>, span=<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>multiTxt, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>,
                     flag=wx.<span style="color: black;">ALL</span>,
                     border=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        bagSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>combo, pos=<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,
                     flag=wx.<span style="color: black;">ALL</span>,
                     border=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add sub-sizers to topSizer</span>
        topSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>titleSizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        topSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">StaticLine</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">panel</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        topSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>bagSizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">panel</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>topSizer<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># SetSizeHints(minW, minH, maxW, maxH)</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizeHints</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span>,<span style="color: #ff4500;">200</span>,<span style="color: #ff4500;">700</span>,<span style="color: #ff4500;">300</span><span style="color: black;">&#41;</span>
        topSizer.<span style="color: black;">Fit</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Run the program</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    app = wx.<span style="color: black;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame = MyForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>One of the first things you&#8217;ll notice is that when I add my icon and label to the GridBagSizer, I make sure to pass the wx.ALIGN_CENTER_VERTICAL flag so that they will be centered with the first text control. You&#8217;ll also notice the &#8220;pos&#8221; parameter. This tells the GridBagSizer where to put the widget. So pos=(0,0) means to put the widget in row 0, column 0.</p>
<p>Other things to note:</p>
<ul>
<li>The first text control gets a border of 10 so it&#8217;s not &#8220;fat&#8221; (too high)</li>
<li>To allow the text control to resize, you need to make the column &#8220;growable&#8221; by using the AddGrowableCol() method of the GridBagSizer.</li>
<li>Take note of which widgets I gave the span attribute to. Remember, span(row,col) starts wherever the widget it applies to is. So if the widget is at (0,0) and you tell it to span(2,0), it will span 2 rows, but zero columns.</li>
</ul>
<p>I didn&#8217;t use all of the GridBagSizer&#8217;s methods in this article. I encourage the reader to check out the methods SetFlexibleDirection() and SetNonFlexibleGrowMode() as they can be helpful in controlling which direction a row or column stretches.</p>
<p><strong>Downloads</strong><br />
<a href='http://www.blog.pythonlibrary.org/wp-content/uploads/2008/05/funky_sized_widgets.txt'>Code</a></p>
<p>Further Reading<br />
<a href="http://wiki.wxpython.org/wxGridBagSizer">GridBagSizers on the Wiki</a><br />
<a href="http://www.zetcode.com/wxpython/layout/">ZetCode</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2008/05/22/wxpython-sizers-tutorial-using-a-gridbagsizer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.735 seconds -->

