<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Mouse Vs. The Python</title>
	
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Wed, 01 May 2013 17:47:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheMouseVsThePython" /><feedburner:info uri="themousevsthepython" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Python 101 – Intro to XML Parsing with ElementTree</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/q-CCganNVuU/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/30/python-101-intro-to-xml-parsing-with-elementtree/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 21:55:20 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[XML Parsing Series]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3163</guid>
		<description><![CDATA[If you have followed this blog for a while, you may remember that we&#8217;ve covered several XML parsing libraries that are included with Python. In this article, we&#8217;ll be continuing that series by taking a quick look at the ElementTree library. You will learn how to create an XML file, edit XML and parse the [...]]]></description>
				<content:encoded><![CDATA[<p>If you have followed this blog for a while, you may remember that we&#8217;ve covered several XML parsing libraries that are included with Python. In this article, we&#8217;ll be continuing that series by taking a quick look at the ElementTree library. You will learn how to create an XML file, edit XML and parse the XML. For comparison&#8217;s sake, we&#8217;ll use the same XML we used in the previous <a href="http://www.blog.pythonlibrary.org/2010/11/12/python-parsing-xml-with-minidom/" target="_blank">minidom article</a> to illustrate the differences between using minidom and ElementTree. Here is the original XML:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;zAppointments</span> <span style="color: #000066;">reminder</span>=<span style="color: #ff0000;">&quot;15&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181251680<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>040000008200E000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181572063<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/state<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bring pizza home<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/zAppointments<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Now let&#8217;s dig into the Python!<span id="more-3163"></span></p>
<h3>How to Create XML with ElementTree</h3>
<p>
<p>Creating XML with ElementTree is very simple. In this section, we will attempt to create the XML above with Python. Here&#8217;s the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span>.<span style="color: black;">ElementTree</span> as <span style="color: #dc143c;">xml</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> createXML<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create an example XML file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    root = <span style="color: #dc143c;">xml</span>.<span style="color: black;">Element</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;zAppointments&quot;</span><span style="color: black;">&#41;</span>
    appt = <span style="color: #dc143c;">xml</span>.<span style="color: black;">Element</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;appointment&quot;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">append</span><span style="color: black;">&#40;</span>appt<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># add appointment children</span>
    begin = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;begin&quot;</span><span style="color: black;">&#41;</span>
    begin.<span style="color: black;">text</span> = <span style="color: #483d8b;">&quot;1181251680&quot;</span>
&nbsp;
    uid = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;uid&quot;</span><span style="color: black;">&#41;</span>
    uid.<span style="color: black;">text</span> = <span style="color: #483d8b;">&quot;040000008200E000&quot;</span>
&nbsp;
    alarmTime = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;alarmTime&quot;</span><span style="color: black;">&#41;</span>
    alarmTime.<span style="color: black;">text</span> = <span style="color: #483d8b;">&quot;1181572063&quot;</span>
&nbsp;
    state = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;state&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    location = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;location&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    duration = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;duration&quot;</span><span style="color: black;">&#41;</span>
    duration.<span style="color: black;">text</span> = <span style="color: #483d8b;">&quot;1800&quot;</span>
&nbsp;
    subject = <span style="color: #dc143c;">xml</span>.<span style="color: black;">SubElement</span><span style="color: black;">&#40;</span>appt, <span style="color: #483d8b;">&quot;subject&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    tree = <span style="color: #dc143c;">xml</span>.<span style="color: black;">ElementTree</span><span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
    with <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span> as fh:
        tree.<span style="color: black;">write</span><span style="color: black;">&#40;</span>fh<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>:
    createXML<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;appt.xml&quot;</span><span style="color: black;">&#41;</span></pre>
<p>If you run this code, you should get something like the following (probably all on one line):</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;zAppointments<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181251680<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>040000008200E000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181572063<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/zAppointments<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>This is pretty close to the original and is certainly valid XML, but it&#8217;s not quite the same. However, it&#8217;s close enough. Let&#8217;s take a moment to review the code and make sure we understand it. First we create the root element by using ElementTree&#8217;s <strong>Element</strong> function. Then we create an appointment element and append it to the root. Next we create SubElements by passing the appointment Element object (appt) to SubElement along with a name, like &#8220;begin&#8221;. Then for each SubElement, we set its <strong>text</strong> property to give it a value. At the end of the script, we create an ElementTree and use it to write the XML out to a file.</p>
<p>What&#8217;s annoying is that it write out the XML all on one line instead of in a nice readable format (i.e. &#8220;pretty print&#8221;). There&#8217;s a <a href="http://effbot.org/zone/element-lib.htm#prettyprint" target="_blank">recipe </a>on Effbot, but there doesn&#8217;t appear to be a way to do it internally. You may also want to take a look at some of the other solutions on <a href="http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python" target="_blank">StackOverflow</a>. It should be noted that lxml supports &#8220;pretty print&#8221; out of the box.</p>
<p>Now we&#8217;re ready to learn how to edit the file!</p>
<h3>How to Edit XML with ElementTree</h3>
<p>
<p>Editing XML with ElementTree is also easy. To make things a little more interesting though, we&#8217;ll add another appointment block to the XML:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;zAppointments</span> <span style="color: #000066;">reminder</span>=<span style="color: #ff0000;">&quot;15&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181251680<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>040000008200E000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181572063<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/state<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bring pizza home<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181253977<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>sdlkjlkadhdakhdfd<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181588888<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state<span style="font-weight: bold; color: black;">&gt;</span></span></span>TX<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/state<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location<span style="font-weight: bold; color: black;">&gt;</span></span></span>Dallas<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bring pizza home<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/zAppointments<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Now let&#8217;s write some code to change each of the <strong>begin</strong> tag&#8217;s values from seconds since the epoch to something a little more readable. We&#8217;ll use Python&#8217;s time module to facilitate this:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span>.<span style="color: black;">cElementTree</span> as ET
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> editXML<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Edit an example XML file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    tree = ET.<span style="color: black;">ElementTree</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span>=filename<span style="color: black;">&#41;</span>
    root = tree.<span style="color: black;">getroot</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> begin_time <span style="color: #ff7700;font-weight:bold;">in</span> root.<span style="color: #008000;">iter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;begin&quot;</span><span style="color: black;">&#41;</span>:
        begin_time.<span style="color: black;">text</span> = <span style="color: #dc143c;">time</span>.<span style="color: black;">ctime</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>begin_time.<span style="color: black;">text</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    tree = ET.<span style="color: black;">ElementTree</span><span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
    with <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;updated.xml&quot;</span>, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span> as f:
        tree.<span style="color: black;">write</span><span style="color: black;">&#40;</span>f<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>:
    editXML<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;original_appt.xml&quot;</span><span style="color: black;">&#41;</span></pre>
<p>Here we create an ElementTree object (tree) and we extract the <strong>root </strong>from it. Then we use ElementTree&#8217;s <strong>iter</strong>() method to find all the tags that are labeled &#8220;begin&#8221;. Note that the iter() method was added in Python 2.7. In our <strong>for </strong>loop, we set each item&#8217;s <strong>text</strong> property to a more human readable time format via <strong>time.ctime</strong>(). You&#8217;ll note that we had to convert the string to an integer when passing it to ctime. The output should look something like the following:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;zAppointments</span> <span style="color: #000066;">reminder</span>=<span style="color: #ff0000;">&quot;15&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>Thu Jun 07 16:28:00 2007<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>040000008200E000<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181572063<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bring pizza home<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>Thu Jun 07 17:06:17 2007<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/begin<span style="font-weight: bold; color: black;">&gt;</span></span></span>        
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>sdlkjlkadhdakhdfd<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/uid<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>1181588888<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/alarmTime<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;state<span style="font-weight: bold; color: black;">&gt;</span></span></span>TX<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/state<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;location<span style="font-weight: bold; color: black;">&gt;</span></span></span>Dallas<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/location<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>1800<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/duration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>Bring pizza home<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/subject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/appointment<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/zAppointments<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can also use ElementTree&#8217;s <strong>find</strong>() or <strong>findall</strong>() methods to get search for specific tags in your XML. The find() method will just find the first instance whereas the findall() will find all the tags with the specified label. These are helpful for editing purposes or for parsing, which is our next topic!</p>
<h3>How to Parse XML with ElementTree</h3>
<p>
<p>Now we get to learn how to do some basic parsing with ElementTree. First we&#8217;ll read through the code and then we&#8217;ll go through bit by bit so we can understand it. Note that this code is based around the original example, but it should work on the second one as well.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span>.<span style="color: black;">cElementTree</span> as ET
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> parseXML<span style="color: black;">&#40;</span>xml_file<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Parse XML with ElementTree
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    tree = ET.<span style="color: black;">ElementTree</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span>=xml_file<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> tree.<span style="color: black;">getroot</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    root = tree.<span style="color: black;">getroot</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;tag=%s, attrib=%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>root.<span style="color: black;">tag</span>, root.<span style="color: black;">attrib</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> child <span style="color: #ff7700;font-weight:bold;">in</span> root:
        <span style="color: #ff7700;font-weight:bold;">print</span> child.<span style="color: black;">tag</span>, child.<span style="color: black;">attrib</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> child.<span style="color: black;">tag</span> == <span style="color: #483d8b;">&quot;appointment&quot;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> step_child <span style="color: #ff7700;font-weight:bold;">in</span> child:
                <span style="color: #ff7700;font-weight:bold;">print</span> step_child.<span style="color: black;">tag</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># iterate over the entire tree</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">40</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Iterating using a tree iterator&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">40</span>
    iter_ = tree.<span style="color: black;">getiterator</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> elem <span style="color: #ff7700;font-weight:bold;">in</span> iter_:
        <span style="color: #ff7700;font-weight:bold;">print</span> elem.<span style="color: black;">tag</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># get the information via the children!</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">40</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Iterating using getchildren()&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">40</span>
    appointments = root.<span style="color: black;">getchildren</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> appointment <span style="color: #ff7700;font-weight:bold;">in</span> appointments:
        appt_children = appointment.<span style="color: black;">getchildren</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> appt_child <span style="color: #ff7700;font-weight:bold;">in</span> appt_children:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s=%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>appt_child.<span style="color: black;">tag</span>, appt_child.<span style="color: black;">text</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>:
    parseXML<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;appt.xml&quot;</span><span style="color: black;">&#41;</span></pre>
<p>You may have already noticed this, but in this example and the last one, we&#8217;ve been importing cElementTree instead of the normal ElementTree. The main difference between the two is that cElementTree is C-based instead of Python-based, so it&#8217;s much faster. Anyway, once again we create an ElementTree object and extract the root from it. You&#8217;ll note that e print out the root and the root&#8217;s tag and attributes. Next we show several ways of iterating over the tags. The first loop just iterates over the XML child by child. This would only print out the top level child (appointment) though, so we added an if statement to check for that child and iterate over its children too.</p>
<p>Next we grab an iterator from the tree object itself and iterate over it that way. You get the same information, but without the extra steps in the first example. The third method uses the root&#8217;s <strong>getchildren</strong>() function. Here again we need an inner loop to grab all the children inside each appointment tag. The last example uses the root&#8217;s iter() method to just loop over any tags that match the string &#8220;begin&#8221;. </p>
<p>As mentioned in the last section, you could also use find() or findall() to help you find specific tags or sets of tags respectively. Also note that each Element object has a <strong>tag</strong> and a <strong>text</strong> property that you can use to acquire that exact information.</p>
<h3>Wrapping Up</h3>
<p>
<p>Now you know how to use ElementTree to create, edit and parse XML. You can add that information to your XML parsing toolkit and use it for fun or profit. You will find links to previous articles on some of the other XML parsing tools below as well as additional information about ElementTree itself. </p>
<h3>Related Articles from Mouse Vs Python</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/2010/11/12/python-parsing-xml-with-minidom/" target="_blank">Parsing XML with minidom</a></li>
<li>Python: <a href="http://www.blog.pythonlibrary.org/2010/11/20/python-parsing-xml-with-lxml/" target="_blank">Parsing XML with lxml</a></li>
<li><a href="http://www.blog.pythonlibrary.org/2012/06/06/parsing-xml-with-python-using-lxml-objectify/" target="_blank">Parsing XML with Python using lxml.objectify</a></li>
</ul>
<h3>Additional Reading</h3>
<p>
<ul>
<li>Official Python documentation for <a href="http://docs.python.org/2/library/xml.etree.elementtree.html" target="_blank">ElementTree</a></li>
<li>Effbot&#8217;s <a href="http://effbot.org/zone/element-index.htm" target="_blank">ElementTree Overview</a></li>
<li><a href="http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/" target="_blank">Processing XML in Python with elementree</a></li>
<li>Dr Chuck&#8217;s <a href="http://www.dr-chuck.com/csev-blog/2008/09/a-simple-python-elementtree-example/" target="_blank">Simple ElementTree Example</a></li>
</ul>
<h3>Download the Source</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/ETXMLParsing.zip">ETXMLParsing.zip</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/q-CCganNVuU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/30/python-101-intro-to-xml-parsing-with-elementtree/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/30/python-101-intro-to-xml-parsing-with-elementtree/</feedburner:origLink></item>
		<item>
		<title>wxPython – Having Fun with Silly Ciphers</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/HUnhlFS7aRg/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/26/wxpython-having-fun-with-silly-ciphers/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 13:19:10 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3153</guid>
		<description><![CDATA[When I was a kid, I was really into secret codes and ciphers. I thought they were all kinds of fun. My mom thought it would be fun to use some of the ciphers I was so enamored with in treasure hunts for special occasions, like birthdays. She would take something like a Cryptograph Wheel [...]]]></description>
				<content:encoded><![CDATA[<p>When I was a kid, I was really into secret codes and ciphers. I thought they were all kinds of fun. My mom thought it would be fun to use some of the ciphers I was so enamored with in treasure hunts for special occasions, like birthdays. She would take something like a Cryptograph Wheel and create codes with it that my brother and I would have to decode with our own wheel to find a gift or another clue. We used stuff where numbers would represent letters (a=1, b=2, c=3) or we would use a sliding scale where you move the alphabet one letter over so A=B, C=D, D=E, etc. Sometimes we&#8217;d create a code stick where you get a long string of paper and wrap it around a pencil and then write a message of the paper. It&#8217;s pretty much impossible to read when it&#8217;s unwrapped.</p>
<p>Anyway, I decided to create a silly cipher program with wxPython where I could input a string and have it convert it to something else. I also wanted my program to decode it too. Now you can&#8217;t really make a program that can use a Cryptograph Wheel or a code stick, but for number codes or slide scales, that&#8217;s extremely easy. <span id="more-3153"></span></p>
<h3>Creating an Encoding GUI</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/cipher_trans.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/cipher_trans-300x225.png" alt="cipher_trans" width="300" height="225" class="aligncenter size-medium wp-image-3154" /></a></p>
<p>Creating the actual GUI is a piece of cake. It takes a little more work to code up the back end where you have to actually parse the string and change it into something else. For this exercise, I created 4 encoders and 3 decoders. The 5 encoders encode strings into numbers, ASCII (which is a different set of numbers), L33t (just for fun), and Hex. For the decoders, I decode everything except for L33t. Let&#8217;s take a moment to study the code for the GUI:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> controller
<span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> CipherPanel<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: black;">&#41;</span>
        choices = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #483d8b;">&quot;Alpha2Num&quot;</span>, <span style="color: #483d8b;">&quot;ASCII&quot;</span>, <span style="color: #483d8b;">&quot;L33t&quot;</span>, <span style="color: #483d8b;">&quot;Hex&quot;</span><span style="color: black;">&#93;</span>
        decode_choices = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #483d8b;">&quot;ASCII&quot;</span>, <span style="color: #483d8b;">&quot;Hex&quot;</span>, <span style="color: #483d8b;">&quot;Num2Alpha&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span> = 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>
&nbsp;
        toLbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Translate into:&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toCbo</span> = wx.<span style="color: black;">ComboBox</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value=choices<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, choices=choices<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">add2HSizer</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>toLbl, <span style="color: #008000;">self</span>.<span style="color: black;">toCbo</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        fromLbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Translate from:&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromCbo</span> = wx.<span style="color: black;">ComboBox</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value=<span style="color: #483d8b;">&quot;&quot;</span>, choices=decode_choices<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">add2HSizer</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>fromLbl, <span style="color: #008000;">self</span>.<span style="color: black;">fromCbo</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        txtLbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Enter text to encode or decode:&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">originalTxt</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_MULTILINE</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">translatedTxt</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_MULTILINE</span><span style="color: black;">&#41;</span>
&nbsp;
        encodeBtn = 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;Encode&quot;</span><span style="color: black;">&#41;</span>
        encodeBtn.<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;">onEncode</span><span style="color: black;">&#41;</span>
        decodeBtn = 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;Decode&quot;</span><span style="color: black;">&#41;</span>
        decodeBtn.<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;">onDecode</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout widgets</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>txtLbl, <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>
        <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">originalTxt</span>, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">EXPAND</span>|wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">translatedTxt</span>, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">EXPAND</span>|wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">add2HSizer</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>encodeBtn, decodeBtn<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</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> add2HSizer<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, widgets<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Add widgets to a horizontal sizer
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        sizer = 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>
        <span style="color: #ff7700;font-weight:bold;">for</span> widget <span style="color: #ff7700;font-weight:bold;">in</span> widgets:
            sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>widget, <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>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>widgets<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, wx.<span style="color: black;">Button</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">mainSizer</span>.<span style="color: black;">Add</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> onDecode<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;
        Decodes what's in the original text box to the encoding 
        specified and puts the result in the bottom text box
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        from_value = <span style="color: #008000;">self</span>.<span style="color: black;">fromCbo</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        value_to_decode = <span style="color: #008000;">self</span>.<span style="color: black;">originalTxt</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> from_value == <span style="color: #483d8b;">&quot;Hex&quot;</span>:
            new_value = value_to_decode.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;hex&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> from_value == <span style="color: #483d8b;">&quot;ASCII&quot;</span>:
            value_to_decode = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> value_to_decode.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
            new_value = controller.<span style="color: black;">convertFromASCII</span><span style="color: black;">&#40;</span>value_to_decode<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> from_value == <span style="color: #483d8b;">&quot;Num2Alpha&quot;</span>:
            value_to_decode = value_to_decode.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            new_value = controller.<span style="color: black;">convertFromNumbers</span><span style="color: black;">&#40;</span>value_to_decode<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">translatedTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>new_value<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onEncode<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;
        Encodes what's in the original text box to the encoding 
        specified and puts the result in the bottom text box
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        to_value = <span style="color: #008000;">self</span>.<span style="color: black;">toCbo</span>.<span style="color: black;">GetValue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        value_to_encode = <span style="color: #008000;">self</span>.<span style="color: black;">originalTxt</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> to_value == <span style="color: #483d8b;">&quot;Hex&quot;</span>:
            new_value = value_to_encode.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;hex&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> to_value == <span style="color: #483d8b;">&quot;ASCII&quot;</span>:
            new_value = controller.<span style="color: black;">convertToASCII</span><span style="color: black;">&#40;</span>value_to_encode<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> to_value == <span style="color: #483d8b;">&quot;L33t&quot;</span>:
            new_value = controller.<span style="color: black;">convertToLeet</span><span style="color: black;">&#40;</span>value_to_encode<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> to_value == <span style="color: #483d8b;">&quot;Alpha2Num&quot;</span>:
            new_value = controller.<span style="color: black;">convertToNumbers</span><span style="color: black;">&#40;</span>value_to_encode<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">translatedTxt</span>.<span style="color: black;">SetValue</span><span style="color: black;">&#40;</span>new_value<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> CipherFrame<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;&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><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;Cipher Creator / Translator&quot;</span>
        size = <span style="color: black;">&#40;</span><span style="color: #ff4500;">800</span>,<span style="color: #ff4500;">600</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>, title=title, size=size<span style="color: black;">&#41;</span>
        panel = CipherPanel<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;">Show</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;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">App</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    frame = CipherFrame<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>We should probably take a couple moments and break this down. As you probably noticed in the screenshot earlier, this GUI has a couple of ComboBoxes, a couple multiline TextCtrls and two buttons. The ComboBoxes control what we&#8217;re encoding or decoding to. We have imported a mysterious module called &#8220;controller&#8221; which houses all the code that does the encoding. We&#8217;ll look at that in a minute. First, we need to look at a couple of the functions in this piece. In the <strong>onEncode</strong> method, we should how to grab the value we&#8217;re going to encode and pass it to the appropriate controller function. For the <strong>onDecode</strong> method, we sometimes have to do a little pre-processing before we pass the data on to the controller. You can see an example in the ASCII portion of the conditional where we have to create a list of integers or in the Num2Alpha section where we need to create a list of numbers. Once you understand what&#8217;s going on here, feel free to continue to the next piece of code below.</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertToASCII<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</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>
    output = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>:
        output.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>letter<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> output<span style="color: black;">&#93;</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> convertToCaesar<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;

http://www.wikihow.com/Create-Secret-Codes-and-Ciphers

    Shifts the alphabet 3 places such that A becomes X,
    B becomes Y, etc
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    caesar_dict = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;a&quot;</span>: <span style="color: #483d8b;">&quot;x&quot;</span>, 
                   <span style="color: #483d8b;">&quot;b&quot;</span>: <span style="color: #483d8b;">&quot;y&quot;</span>,
                   <span style="color: #483d8b;">&quot;c&quot;</span>: <span style="color: #483d8b;">&quot;z&quot;</span>,
                   <span style="color: #483d8b;">&quot;d&quot;</span>: <span style="color: #483d8b;">&quot;a&quot;</span>,
                   <span style="color: #483d8b;">&quot;e&quot;</span>: <span style="color: #483d8b;">&quot;b&quot;</span>,
                   <span style="color: #483d8b;">&quot;f&quot;</span>: <span style="color: #483d8b;">&quot;c&quot;</span>,
                   <span style="color: #483d8b;">&quot;g&quot;</span>: <span style="color: #483d8b;">&quot;d&quot;</span>,
                   <span style="color: #483d8b;">&quot;h&quot;</span>: <span style="color: #483d8b;">&quot;e&quot;</span>,
                   <span style="color: #483d8b;">&quot;i&quot;</span>: <span style="color: #483d8b;">&quot;f&quot;</span>,
                   <span style="color: #483d8b;">&quot;j&quot;</span>: <span style="color: #483d8b;">&quot;g&quot;</span>,
                   <span style="color: #483d8b;">&quot;k&quot;</span>: <span style="color: #483d8b;">&quot;h&quot;</span>,
                   <span style="color: #483d8b;">&quot;l&quot;</span>: <span style="color: #483d8b;">&quot;i&quot;</span>,
                   <span style="color: #483d8b;">&quot;m&quot;</span>: <span style="color: #483d8b;">&quot;j&quot;</span>,
                   <span style="color: #483d8b;">&quot;n&quot;</span>: <span style="color: #483d8b;">&quot;k&quot;</span>,
                   <span style="color: #483d8b;">&quot;o&quot;</span>: <span style="color: #483d8b;">&quot;l&quot;</span>,
                   <span style="color: #483d8b;">&quot;p&quot;</span>: <span style="color: #483d8b;">&quot;m&quot;</span>,
                   <span style="color: #483d8b;">&quot;q&quot;</span>: <span style="color: #483d8b;">&quot;n&quot;</span>,
                   <span style="color: #483d8b;">&quot;r&quot;</span>: <span style="color: #483d8b;">&quot;o&quot;</span>,
                   <span style="color: #483d8b;">&quot;s&quot;</span>: <span style="color: #483d8b;">&quot;p&quot;</span>,
                   <span style="color: #483d8b;">&quot;t&quot;</span>: <span style="color: #483d8b;">&quot;q&quot;</span>,
                   <span style="color: #483d8b;">&quot;u&quot;</span>: <span style="color: #483d8b;">&quot;r&quot;</span>,
                   <span style="color: #483d8b;">&quot;v&quot;</span>: <span style="color: #483d8b;">&quot;s&quot;</span>,
                   <span style="color: #483d8b;">&quot;w&quot;</span>: <span style="color: #483d8b;">&quot;t&quot;</span>,
                   <span style="color: #483d8b;">&quot;x&quot;</span>: <span style="color: #483d8b;">&quot;u&quot;</span>,
                   <span style="color: #483d8b;">&quot;y&quot;</span>: <span style="color: #483d8b;">&quot;v&quot;</span>,
                   <span style="color: #483d8b;">&quot;z&quot;</span>: <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#125;</span>
    new_string = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> char == <span style="color: #483d8b;">' '</span>:
            new_string += <span style="color: #483d8b;">' '</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            new_string += caesar_dict<span style="color: black;">&#91;</span>char.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> new_string
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertToLeet<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</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>
    leet_dict = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;a&quot;</span>:<span style="color: #483d8b;">&quot;4&quot;</span>, <span style="color: #483d8b;">&quot;b&quot;</span>:<span style="color: #483d8b;">&quot;8&quot;</span>, <span style="color: #483d8b;">&quot;e&quot;</span>:<span style="color: #483d8b;">&quot;3&quot;</span>, <span style="color: #483d8b;">&quot;l&quot;</span>:<span style="color: #483d8b;">&quot;1&quot;</span>,
                 <span style="color: #483d8b;">&quot;o&quot;</span>:<span style="color: #483d8b;">&quot;0&quot;</span>, <span style="color: #483d8b;">&quot;s&quot;</span>:<span style="color: #483d8b;">&quot;5&quot;</span>, <span style="color: #483d8b;">&quot;t&quot;</span>:<span style="color: #483d8b;">&quot;7&quot;</span><span style="color: black;">&#125;</span>
    new_string = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> letter.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> leet_dict:
            letter = leet_dict<span style="color: black;">&#91;</span>letter.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        new_string += letter
    <span style="color: #ff7700;font-weight:bold;">return</span> new_string
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertToNumbers<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Convert a string to numbers where a=1, b=2, c=3, etc
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    keys = <span style="color: #483d8b;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span>
    values = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'1'</span>, <span style="color: #483d8b;">'2'</span>, <span style="color: #483d8b;">'3'</span>, <span style="color: #483d8b;">'4'</span>, <span style="color: #483d8b;">'5'</span>, <span style="color: #483d8b;">'6'</span>, <span style="color: #483d8b;">'7'</span>, <span style="color: #483d8b;">'8'</span>, <span style="color: #483d8b;">'9'</span>, <span style="color: #483d8b;">'10'</span>, <span style="color: #483d8b;">'11'</span>,
              <span style="color: #483d8b;">'12'</span>, <span style="color: #483d8b;">'13'</span>, <span style="color: #483d8b;">'14'</span>, <span style="color: #483d8b;">'15'</span>, <span style="color: #483d8b;">'16'</span>, <span style="color: #483d8b;">'17'</span>, <span style="color: #483d8b;">'18'</span>, <span style="color: #483d8b;">'19'</span>, <span style="color: #483d8b;">'20'</span>, 
              <span style="color: #483d8b;">'21'</span>, <span style="color: #483d8b;">'22'</span>, <span style="color: #483d8b;">'23'</span>, <span style="color: #483d8b;">'24'</span>, <span style="color: #483d8b;">'25'</span>, <span style="color: #483d8b;">'26'</span><span style="color: black;">&#93;</span>
    num_dict = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: #008000;">zip</span><span style="color: black;">&#40;</span>keys,values<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    new_string = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> letter.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> num_dict:
            new_string += num_dict<span style="color: black;">&#91;</span>letter.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot; &quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> new_string
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertFromASCII<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Convert from ASCII
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    s = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>data, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            s += <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># can also convert a list of integers</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> data:
            s += <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> s
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertFromCaesar<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Converts string from Caesar to normal
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    uncaesar_dict = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;x&quot;</span>: <span style="color: #483d8b;">&quot;a&quot;</span>,
                     <span style="color: #483d8b;">&quot;y&quot;</span>: <span style="color: #483d8b;">&quot;b&quot;</span>,
                     <span style="color: #483d8b;">&quot;z&quot;</span>: <span style="color: #483d8b;">&quot;c&quot;</span>,
                     <span style="color: #483d8b;">&quot;a&quot;</span>: <span style="color: #483d8b;">&quot;d&quot;</span>,
                     <span style="color: #483d8b;">&quot;b&quot;</span>: <span style="color: #483d8b;">&quot;e&quot;</span>,
                     <span style="color: #483d8b;">&quot;c&quot;</span>: <span style="color: #483d8b;">&quot;f&quot;</span>,
                     <span style="color: #483d8b;">&quot;d&quot;</span>: <span style="color: #483d8b;">&quot;g&quot;</span>,
                     <span style="color: #483d8b;">&quot;e&quot;</span>: <span style="color: #483d8b;">&quot;h&quot;</span>,
                     <span style="color: #483d8b;">&quot;f&quot;</span>: <span style="color: #483d8b;">&quot;i&quot;</span>,
                     <span style="color: #483d8b;">&quot;g&quot;</span>: <span style="color: #483d8b;">&quot;j&quot;</span>,
                     <span style="color: #483d8b;">&quot;h&quot;</span>: <span style="color: #483d8b;">&quot;k&quot;</span>,
                     <span style="color: #483d8b;">&quot;i&quot;</span>: <span style="color: #483d8b;">&quot;l&quot;</span>,
                     <span style="color: #483d8b;">&quot;j&quot;</span>: <span style="color: #483d8b;">&quot;m&quot;</span>,
                     <span style="color: #483d8b;">&quot;k&quot;</span>: <span style="color: #483d8b;">&quot;n&quot;</span>,
                     <span style="color: #483d8b;">&quot;l&quot;</span>: <span style="color: #483d8b;">&quot;o&quot;</span>,
                     <span style="color: #483d8b;">&quot;m&quot;</span>: <span style="color: #483d8b;">&quot;p&quot;</span>,
                     <span style="color: #483d8b;">&quot;n&quot;</span>: <span style="color: #483d8b;">&quot;q&quot;</span>,
                     <span style="color: #483d8b;">&quot;o&quot;</span>: <span style="color: #483d8b;">&quot;r&quot;</span>,
                     <span style="color: #483d8b;">&quot;p&quot;</span>: <span style="color: #483d8b;">&quot;s&quot;</span>,
                     <span style="color: #483d8b;">&quot;q&quot;</span>: <span style="color: #483d8b;">&quot;t&quot;</span>,
                     <span style="color: #483d8b;">&quot;r&quot;</span>: <span style="color: #483d8b;">&quot;u&quot;</span>,
                     <span style="color: #483d8b;">&quot;s&quot;</span>: <span style="color: #483d8b;">&quot;v&quot;</span>,
                     <span style="color: #483d8b;">&quot;t&quot;</span>: <span style="color: #483d8b;">&quot;w&quot;</span>,
                     <span style="color: #483d8b;">&quot;u&quot;</span>: <span style="color: #483d8b;">&quot;x&quot;</span>,
                     <span style="color: #483d8b;">&quot;v&quot;</span>: <span style="color: #483d8b;">&quot;y&quot;</span>,
                     <span style="color: #483d8b;">&quot;w&quot;</span>: <span style="color: #483d8b;">&quot;z&quot;</span><span style="color: black;">&#125;</span>
    new_string = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">string</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> char == <span style="color: #483d8b;">' '</span>:
            new_string += <span style="color: #483d8b;">' '</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            new_string += uncaesar_dict<span style="color: black;">&#91;</span>char.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> new_string
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> convertFromNumbers<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Convert a list of numbers to letters where 1=a, 2=b, 3=c, etc
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    keys = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'1'</span>, <span style="color: #483d8b;">'2'</span>, <span style="color: #483d8b;">'3'</span>, <span style="color: #483d8b;">'4'</span>, <span style="color: #483d8b;">'5'</span>, <span style="color: #483d8b;">'6'</span>, <span style="color: #483d8b;">'7'</span>, <span style="color: #483d8b;">'8'</span>, <span style="color: #483d8b;">'9'</span>, <span style="color: #483d8b;">'10'</span>, <span style="color: #483d8b;">'11'</span>,
            <span style="color: #483d8b;">'12'</span>, <span style="color: #483d8b;">'13'</span>, <span style="color: #483d8b;">'14'</span>, <span style="color: #483d8b;">'15'</span>, <span style="color: #483d8b;">'16'</span>, <span style="color: #483d8b;">'17'</span>, <span style="color: #483d8b;">'18'</span>, <span style="color: #483d8b;">'19'</span>, <span style="color: #483d8b;">'20'</span>, 
            <span style="color: #483d8b;">'21'</span>, <span style="color: #483d8b;">'22'</span>, <span style="color: #483d8b;">'23'</span>, <span style="color: #483d8b;">'24'</span>, <span style="color: #483d8b;">'25'</span>, <span style="color: #483d8b;">'26'</span><span style="color: black;">&#93;</span>
    values = <span style="color: #483d8b;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span>
    num_dict = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: #008000;">zip</span><span style="color: black;">&#40;</span>keys,values<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    new_string = <span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> data:
        val = num_dict<span style="color: black;">&#91;</span>char<span style="color: black;">&#93;</span>
        new_string += val
    <span style="color: #ff7700;font-weight:bold;">return</span> new_string
&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>:
    x = convertToASCII<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;I love you&quot;</span><span style="color: black;">&#41;</span>
    y = convertToLeet<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;I love you&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> x
    new_x = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> x.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> convertFromASCII<span style="color: black;">&#40;</span>new_x<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> convertFromASCII<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
&nbsp;
    x = convertToCaesar<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Meeting tomorrow at station&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Meeting tomorrow at station =&gt;&quot;</span>, x
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s =&gt;&quot;</span> <span style="color: #66cc66;">%</span> x, convertFromCaesar<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
&nbsp;
    t = convertToNumbers<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Python rules&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Python rules =&gt; &quot;</span> + t
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s =&gt; &quot;</span> <span style="color: #66cc66;">%</span> t, convertFromNumbers<span style="color: black;">&#40;</span>t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
<p>You may notice that I have included other examples in the controller that aren&#8217;t currently hooked into the GUI. For example, I have a converter function in there to convert strings to Caesar, which is a popular cipher where the alphabet is shifted 3 places in one direction or the other. Anyway, one of the nicest bits about this code is that we didn&#8217;t need to import anything. It all works with just normal Python! To convert to ASCII, we use Python&#8217;s builtin <strong>ord</strong>. For most of the others, we use dictionaries to map the values. At the bottom of the script, we have a few test cases to check and make sure it is converting the strings as we expect it to. These should probably be made into real unit tests, but for a quick and dirty check, these work great!</p>
<h3>Wrapping Up</h3>
<p>
<p>There are several other codes I&#8217;d like to add to this at some point, such as Morse code. I also like the codes where the message is hidden in the text, such as every first letter (or last) in a poem spells our something or where every letter at the beginning of a sentence spells something. Those were always fun. I&#8217;ve included a few links at the end of this article so you can read about other fun codes and ciphers. Have fun coding!</p>
<h3>Additional Reading</h3>
<p>
<ul>
<li><a href="https://sites.google.com/site/codesforscouts/" target="_blank">Secret Codes for Cubs and Scouts</a></li>
<li><a href="http://www.cerias.purdue.edu/education/k-12/teaching_resources/lessons_presentations/cryptology.html" target="_blank">Cryptology for Kids</a></li>
<li>The Pig Pen <a href="http://en.wikipedia.org/wiki/Pigpen_cipher" target="_blank">cipher</a> &#8211; this one is fun!</li>
<li>WikiHow &#8211; <a href="http://www.wikihow.com/Create-Secret-Codes-and-Ciphers#Caesar_Cipher" target="_blank">How to Create Secret Codes and Ciphers</a></li>
</ul>
<h3>Download the Source!</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/cipherGUI.zip">cipherGUI.zip</a></li>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/cipherGUI.tar">cipherGUI.tar</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/HUnhlFS7aRg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/26/wxpython-having-fun-with-silly-ciphers/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/26/wxpython-having-fun-with-silly-ciphers/</feedburner:origLink></item>
		<item>
		<title>eBook Review: Real Python</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/J6k_RhNCTXs/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/19/ebook-review-real-python/#comments</comments>
		<pubDate>Sat, 20 Apr 2013 00:24:42 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3141</guid>
		<description><![CDATA[I recently received my copy of Real Python by Fletcher Heisler and just finished reading it. I got it as a bonus from a recent KickStarter campaign for the sequel to the book, Real Python for Web Development which is actually written by someone else. You can go to the book&#8217;s website to purchase the [...]]]></description>
				<content:encoded><![CDATA[<p>I recently received my copy of <em>Real Python</em> by Fletcher Heisler and just finished reading it. I got it as a bonus from a recent <a href="http://www.kickstarter.com/projects/1369857650/real-python-for-web-development-featuring-web2py" target="_blank">KickStarter campaign</a> for the sequel to the book, <em>Real Python for Web Development</em> which is actually written by someone else. You can go to the book&#8217;s <a href="http://www.realpython.com/" target="_blank">website </a>to purchase the book and get access to the files that the book talks about. I&#8217;ll be reviewing revision 2.2, as that was what I got when I downloaded it a couple weeks ago. It appears that there&#8217;s been an update since then though. This is the first Python book I&#8217;ve ever read that focuses on Python 2.7.3, although it does mention some differences in Python 3 as it goes along.</p>
<h3>Quick Review</h3>
<p>
<ul>
<li><strong>Why I picked it up:</strong> I got this book as part of the perks received from its sequel&#8217;s Kickstarter campaign.</li>
<li><strong>Why I finished it:</strong> The book had an engaging writing style </li>
<li><strong>I&#8217;d give it to:</strong> Programmers new to the Python language </li>
</ul>
<p>If that intro interested you at all, feel free to join me after the jump to read the full review!<span id="more-3141"></span></p>
<h3>Book Formats</h3>
<p>
<p>This book is available as a PDF, MOBI (i.e. for Kindles) and EPUB. I don&#8217;t believe there is a dead tree version at the time of this review.</p>
<h3>Full Review</h3>
<p>
<p>Mr. Heisler does a good job introducing us to Python. At first, it sounds like you&#8217;ll be learning Python by writing lots of code (which made me think of Zed Shaw&#8217;s <a href="http://learnpythonthehardway.org/" target="_blank">website</a>), but the author ends up doing what most Python authors do and goes through a bunch of the basics. Anyway, he book is split up into 13 chapters (14 if you count chapter 0!) with some &#8220;interlude&#8221; sections thrown in here and there. Here&#8217;s a breakdown of what&#8217;s covered:</p>
<p>Chapter zero is just an introduction. Chapter one covers downloading Python and the basics of using IDLE. Then we hit an interlude that covers comments in the code. Chapters two and three covers strings and string methods (finding strings, slices, etc). Chapter 4 goes into functions and loops. Then there&#8217;s another interlude, this time covering the Python debugger: pdb. Chapter 5 does conditionals (i.e. if/elif/else) and also talks about how to break and continue in a loop. Chapter 6 is all about lists and dictionaries. In seven, we learn file I/O and how to read and write CSV files. Then we hit another interlude where the author talks about how to install 3rd party Python modules. For some reason, he recommends easy_install, which I thought had fallen out of favor in the Python community. But there&#8217;s no mention of the alternatives, such as pip. Anyway, in chapter 8, we learn how to read, write and manipulate PDFs with pyPDF. There&#8217;s a mention of reportlab and PDFMiner, but no examples. Chapter 9 goes into SQL with Python, mostly focusing on SQLite, although it mentions pyodbc, pyscopg and MySQLdb.</p>
<p>Chapter 10 goes into web scraping with Python regular expressions, beautifulsoup and mechanize. Chapter 11 is about scientific programming with Python and graphing. It covers a little NumPy and a little matplotlib. Chapter 12 dabbles in Python GUI work with EasyGUI and Tkinter. I thought EasyGUI was an odd choice as it&#8217;s basically just a dialog generator, but anyway. The final chapter covers web applications using the Google App Engine.</p>
<p>Each chapter has multiple sections and the sections usually end with an exercise or two for the reader to try out. The text is written in a very informal way with some fun examples, like writing a L33T translator script. Since this was a KickStarter book, the last few pages list all the supporters who helped get the book published. I thought the book covered an adequate amount of the language to get a new programmer started. It&#8217;s not an in-depth Python book, but it&#8217;s not supposed to be. It&#8217;s there to get the reader started on their Python journey and it does a good job doing just that!</p>
<table>
<tr>
<td><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/real_python.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/real_python.png" alt="real_python" width="250" height="146" class="aligncenter size-full wp-image-3148" /></a></td>
<td>
<h3>Real Python</h3>
<p><p>Fletcher Heisler</p>
<p><strong><a href="http://www.realpython.com/">RealPython.com</a></strong></p>
</td>
</tr>
</table>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/J6k_RhNCTXs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/19/ebook-review-real-python/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/19/ebook-review-real-python/</feedburner:origLink></item>
		<item>
		<title>PySide: Standard Dialogs and Message Boxes</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/SzvESyRTedY/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/16/pyside-standard-dialogs-and-message-boxes/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 21:52:43 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[GUI Toolkits]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[PyQT]]></category>
		<category><![CDATA[PySide]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3107</guid>
		<description><![CDATA[The PySide GUI toolkit for Python has several standard dialogs and message boxes that you can use as-is. You can also create custom dialogs, but we&#8217;ll be saving that for a future article. In this post, we will cover the following dialogs: Color Dialog File Dialog Font Dialog Input Dialog Print &#038; Print Preview Dialogs [...]]]></description>
				<content:encoded><![CDATA[<p>The PySide GUI toolkit for Python has several standard dialogs and message boxes that you can use as-is. You can also create custom dialogs, but we&#8217;ll be saving that for a future article. In this post, we will cover the following dialogs:</p>
<ul>
<li>Color Dialog</li>
<li>File Dialog</li>
<li>Font Dialog</li>
<li>Input Dialog</li>
<li>Print &#038; Print Preview Dialogs</li>
</ul>
<p>We will also cover PySide&#8217;s Message Boxes. All code in this article was tested on Windows 7 Professional with PySide 1.2.2 and Python 2.6.6. Now let&#8217;s get to it!<span id="more-3107"></span></p>
<h3>Getting Information with Standard Dialogs</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideStdDlgDemo.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideStdDlgDemo-300x162.png" alt="pysideStdDlgDemo" width="300" height="162" class="aligncenter size-medium wp-image-3115" /></a></p>
<p>We&#8217;ll start off with some code that will show each of the first four dialogs listed above. The other two are more complicated and get their own special demo code. After the code, we&#8217;ll take a look at each dialog&#8217;s code individually to help understand it. </p>
<pre class="python"><span style="color: #808080; font-style: italic;"># stdDialogDemo_pyside.py</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> PySide <span style="color: #ff7700;font-weight:bold;">import</span> QtCore, QtGui
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DialogDemo<span style="color: black;">&#40;</span>QtGui.<span style="color: black;">QWidget</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><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>
        <span style="color: #808080; font-style: italic;"># super(DialogDemo, self).__init__()</span>
        QtGui.<span style="color: black;">QWidget</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>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span> = QtGui.<span style="color: black;">QLabel</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Python rules!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create the buttons</span>
        colorDialogBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Open Color Dialog&quot;</span><span style="color: black;">&#41;</span>
        fileDialogBtn =  QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Open File Dialog&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fontDialogBtn</span> = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Open Font Dialog&quot;</span><span style="color: black;">&#41;</span>
        inputDlgBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Open Input Dialog&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># connect the buttons to the functions (signals to slots)</span>
        colorDialogBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">openColorDialog</span><span style="color: black;">&#41;</span>
        fileDialogBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">openFileDialog</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fontDialogBtn</span>.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">openFontDialog</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>inputDlgBtn, QtCore.<span style="color: black;">SIGNAL</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;clicked()&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">openInputDialog</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout widgets</span>
        layout = QtGui.<span style="color: black;">QVBoxLayout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">label</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>colorDialogBtn<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>fileDialogBtn<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fontDialogBtn</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>inputDlgBtn<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>layout<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># set the position and size of the window</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setGeometry</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">400</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Dialog Demo&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> openColorDialog<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;
        Opens the color dialog
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        color = QtGui.<span style="color: black;">QColorDialog</span>.<span style="color: black;">getColor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> color.<span style="color: black;">isValid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> color.<span style="color: black;">name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            btn = <span style="color: #008000;">self</span>.<span style="color: black;">sender</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            pal = btn.<span style="color: black;">palette</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            pal.<span style="color: black;">setColor</span><span style="color: black;">&#40;</span>QtGui.<span style="color: black;">QPalette</span>.<span style="color: black;">Button</span>, color<span style="color: black;">&#41;</span>
            btn.<span style="color: black;">setPalette</span><span style="color: black;">&#40;</span>pal<span style="color: black;">&#41;</span>
            btn.<span style="color: black;">setAutoFillBackground</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">#btn.setStyleSheet(&quot;QPushButton {background-color: %s}&quot; % color)</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> openFileDialog<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;
        Opens a file dialog and sets the label to the chosen path
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
        path, _ = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">getOpenFileName</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Open File&quot;</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> openDirectoryDialog<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;
        Opens a dialog to allow user to choose a directory
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        flags = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">DontResolveSymlinks</span> | QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">ShowDirsOnly</span>
        d = directory = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">getExistingDirectory</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,
                                                               <span style="color: #483d8b;">&quot;Open Directory&quot;</span>,
                                                               <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
                                                               flags<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> openFontDialog<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;
        Open the QFontDialog and set the label's font
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        font, ok = QtGui.<span style="color: black;">QFontDialog</span>.<span style="color: black;">getFont</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> ok:
            <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<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;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> openInputDialog<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;
        Opens the text version of the input dialog
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        text, result = QtGui.<span style="color: black;">QInputDialog</span>.<span style="color: black;">getText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;I'm a text Input Dialog!&quot;</span>,
                                            <span style="color: #483d8b;">&quot;What is your favorite programming language?&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> result:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You love %s!&quot;</span> <span style="color: #66cc66;">%</span> text
&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 = QtGui.<span style="color: black;">QApplication</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    form = DialogDemo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Well that looks pretty clean and straight-forward. Let&#8217;s break it down a bit. You can see at the beginning of the __init__ method that I have commented out an alternate way of initializing the QWidget using Python&#8217;s super. You can do it either way and your program will work fine. I just wanted to show you both ways of doing it. You&#8217;ll also notice that all this code does is display a QLabel and four QPushButton widgets. We connect the signals (i.e. events) from the buttons to the slots (i.e. event handlers) using two different methods. The first is more &#8220;Pythonic&#8221; in that it uses dot-notation: </p>
<pre class="python">colorDialogBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">openColorDialog</span><span style="color: black;">&#41;</span></pre>
<p>The second way (which we use once) is probably the most common way you&#8217;ll see it done, but it&#8217;s a little harder to understand if you&#8217;re just used to Python code:</p>
<pre class="python"><span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>inputDlgBtn, QtCore.<span style="color: black;">SIGNAL</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;clicked()&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">openInputDialog</span><span style="color: black;">&#41;</span></pre>
<p>Both ways of connecting the signals to the slots works. Note that we&#8217;re binding the <strong>clicked</strong> event. This means that when the user clicks the button, it calls the connected method. Now we&#8217;ll take a look at each of the dialogs in turn.</p>
<h4>The PySide Color Dialog</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideColorDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideColorDlg-300x236.png" alt="pysideColorDlg" width="300" height="236" class="aligncenter size-medium wp-image-3116" /></a></p>
<p>The PySide Color Dialog is a pretty standard color dialog that you&#8217;ll see in most simple drawing programs, such as Windows Paint. To create it, we do the following:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> openColorDialog<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;
    Opens the color dialog
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    color = QtGui.<span style="color: black;">QColorDialog</span>.<span style="color: black;">getColor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> color.<span style="color: black;">isValid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> color.<span style="color: black;">name</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        btn = <span style="color: #008000;">self</span>.<span style="color: black;">sender</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        pal = btn.<span style="color: black;">palette</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        pal.<span style="color: black;">setColor</span><span style="color: black;">&#40;</span>QtGui.<span style="color: black;">QPalette</span>.<span style="color: black;">Button</span>, color<span style="color: black;">&#41;</span>
        btn.<span style="color: black;">setPalette</span><span style="color: black;">&#40;</span>pal<span style="color: black;">&#41;</span>
        btn.<span style="color: black;">setAutoFillBackground</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#btn.setStyleSheet(&quot;QPushButton {background-color: %s}&quot; % color)</span></pre>
<p>Here we call <strong>QtGui.QColorDialog.getColor()</strong> to return the color chosen by the user and to also instantiate the dialog. If the color chosen is valid, we apply it to our QPushButton. You&#8217;ll note that there&#8217;s a line commented out at the end of this method. I was told that you can use a type of PySide CSS to change the background color of the button, but I couldn&#8217;t figure out the correct way to do it. Some people on the PySide IRC channel said the commented line should work. Others said that it should be just <strong>background</strong> instead of <strong>background-color</strong>, but that didn&#8217;t work for me either. Even with the Palette method shown here, the background color still doesn&#8217;t change. Instead, you&#8217;ll only notice a subtle change of color to the outline of the button.</p>
<h4>The PySide File Dialog</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideFileDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideFileDlg-300x187.png" alt="pysideFileDlg" width="300" height="187" class="aligncenter size-medium wp-image-3117" /></a></p>
<p>The PySide File Dialog should be familiar to most people as it matches the native file dialog very closely (on Windows at least). Here&#8217;s the code to instantiate it:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> openFileDialog<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;
    Opens a file dialog and sets the label to the chosen path
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
    path, _ = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">getOpenFileName</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Open File&quot;</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span></pre>
<p>We use <strong>getOpenFileName</strong> to return the file path in a tuple that also includes the file type, which in this case is <strong>All Files (*.*)</strong>. We also set our QLabel to the path that was returned. There&#8217;s also a method in our main code called <strong>openDirectoryDialog</strong> that uses the same call signature but instead of getOpenFileName, we use <strong>getExistingDirectory</strong>. See the example below:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> openDirectoryDialog<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;
    Opens a dialog to allow user to choose a directory
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    flags = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">DontResolveSymlinks</span> | QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">ShowDirsOnly</span>
    d = directory = QtGui.<span style="color: black;">QFileDialog</span>.<span style="color: black;">getExistingDirectory</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,
                                                           <span style="color: #483d8b;">&quot;Open Directory&quot;</span>,
                                                           <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
                                                           flags<span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span></pre>
<p>As you can see, we had to pass a couple of flags to this dialog to tell it not to resolve symlinks and to only show directories. Finally, there is also a <strong>getOpenFileNames</strong> method to the QFileDialog that allows the user to choose multiple files from the dialog and return all the selected files. This one is not shown, but operates similarly.</p>
<h4>The PySide Font Dialog</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideFontDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideFontDlg-300x234.png" alt="pysideFontDlg" width="300" height="234" class="aligncenter size-medium wp-image-3119" /></a></p>
<p>The PySide Font dialog is very similar to the one you&#8217;ll see in older versions of Microsoft Office products. Creating the dialog is pretty simple:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> openFontDialog<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;
    Open the QFontDialog and set the label's font
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    font, ok = QtGui.<span style="color: black;">QFontDialog</span>.<span style="color: black;">getFont</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> ok:
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span></pre>
<p>As you can see, you just create the dialog and call its <strong>getFont()</strong> method. This returns the chosen font and also whether or not the OK button was pushed. If it was, we reset our QLabel widget with the new font.</p>
<h4>The PySide Input Dialog</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideInputDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysideInputDlg-300x148.png" alt="pysideInputDlg" width="300" height="148" class="aligncenter size-medium wp-image-3120" /></a></p>
<p>The PySide Input dialog is also pretty simple. However, there are four varieties: Text, Integer, Double and Item. As is fairly obvious, these return text, integers, doubles (i.e. float) and items, which allows the user to choose from a list of items from a combobox. Here we show how to create the text version:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> openInputDialog<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;
    Opens the text version of the input dialog
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    text, result = QtGui.<span style="color: black;">QInputDialog</span>.<span style="color: black;">getText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;I'm a text Input Dialog!&quot;</span>,
                                        <span style="color: #483d8b;">&quot;What is your favorite programming language?&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> result:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You love %s!&quot;</span> <span style="color: #66cc66;">%</span> text</pre>
<p>If you&#8217;d like to see examples of the other types of input dialogs, I recommend you see the <a href="http://qt.gitorious.org/pyside/pyside-examples/blobs/f6a7054d4e730787ca86a473023aedc4af4ae649/examples/dialogs/standarddialogs.py" target="_blank">pyside-examples folder on git</a>, specifically the <strong>standarddialogs.py</strong> script. You can also see screenshots of all four of these widgets <a href="http://ftp.ics.uci.edu/pub/centos0/ics-custom-build/BUILD/PyQt-x11-gpl-4.7.2/doc/html/qinputdialog.html" target="_blank">here</a>. </p>
<p>At this point, we&#8217;re ready to learn about the two types of print dialogs.</p>
<h3>PySide&#8217;s Print Dialogs</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintDlgDemo.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintDlgDemo-300x175.png" alt="pysidePrintDlgDemo" width="300" height="175" class="aligncenter size-medium wp-image-3133" /></a></p>
<p>For this demo, we create a simple little window with a QTextEdit to hold some example text and two buttons. We&#8217;ll copy and paste the code example from the previous section into this demo app and then press the Print button. Here&#8217;s the demo code:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># printDlgDemo.py</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> PySide <span style="color: #ff7700;font-weight:bold;">import</span> QtGui, QtCore
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> PrinterWindow<span style="color: black;">&#40;</span>QtGui.<span style="color: black;">QWidget</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><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>
        QtGui.<span style="color: black;">QWidget</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: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Printing Demo&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span> = QtGui.<span style="color: black;">QTextEdit</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
        printButton = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Print'</span><span style="color: black;">&#41;</span>
        printButton.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onPrint</span><span style="color: black;">&#41;</span>
        printPreviewButton = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Print Preview'</span><span style="color: black;">&#41;</span>
        printPreviewButton.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onPrintPreview</span><span style="color: black;">&#41;</span>
&nbsp;
        btnLayout = QtGui.<span style="color: black;">QHBoxLayout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        mainLayout = QtGui.<span style="color: black;">QVBoxLayout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        btnLayout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>printButton<span style="color: black;">&#41;</span>
        btnLayout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>printPreviewButton<span style="color: black;">&#41;</span>
        mainLayout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span><span style="color: black;">&#41;</span>
        mainLayout.<span style="color: black;">addLayout</span><span style="color: black;">&#40;</span>btnLayout<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>mainLayout<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onPrint<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 and show the print dialog
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        dialog = QtGui.<span style="color: black;">QPrintDialog</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> dialog.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == QtGui.<span style="color: black;">QDialog</span>.<span style="color: black;">Accepted</span>:
            doc = <span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span>.<span style="color: black;">document</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            doc.<span style="color: black;">print_</span><span style="color: black;">&#40;</span>dialog.<span style="color: black;">printer</span><span style="color: black;">&#40;</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;">def</span> onPrintPreview<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 and show a print preview window
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        dialog = QtGui.<span style="color: black;">QPrintPreviewDialog</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        dialog.<span style="color: black;">paintRequested</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span>.<span style="color: black;">print_</span><span style="color: black;">&#41;</span>
        dialog.<span style="color: black;">exec_</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;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = QtGui.<span style="color: black;">QApplication</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    form = PrinterWindow<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>If you hit the Print button, you should see a dialog similar to the one below (if you&#8217;re on Windows 7):</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintDlg-300x250.png" alt="pysidePrintDlg" width="300" height="250" class="aligncenter size-medium wp-image-3132" /></a></p>
<p>Let&#8217;s take a look at the code behind this dialog:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onPrint<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 and show the print dialog
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    dialog = QtGui.<span style="color: black;">QPrintDialog</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> dialog.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == QtGui.<span style="color: black;">QDialog</span>.<span style="color: black;">Accepted</span>:
        doc = <span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span>.<span style="color: black;">document</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        doc.<span style="color: black;">print_</span><span style="color: black;">&#40;</span>dialog.<span style="color: black;">printer</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
<p>Here we instantiate the <strong>QPrintDialog</strong> and we show it by calling its <strong>exec_()</strong> method. If the user actually hits the Print button, we&#8217;ll extract a <strong>QTextDocument</strong> from the QTextEdit control and then call its <strong>print_()</strong> method by passing it the printer dialog. </p>
<p>Now let&#8217;s do the print preview dialog! Here&#8217;s the code from the demo mentioned earlier:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onPrintPreview<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 and show a print preview window
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    dialog = QtGui.<span style="color: black;">QPrintPreviewDialog</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    dialog.<span style="color: black;">paintRequested</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">text_editor</span>.<span style="color: black;">print_</span><span style="color: black;">&#41;</span>
    dialog.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This code is a little simpler than creating the QPrintDialog. Here we just instantiate an instance of <strong>QPrintPreviewDialog</strong>, tell it we want to connect <strong>paintRequested </strong>signal to the <strong>print_</strong> method of text control. Finally we show the dialog. PySide does the rest and ends up creating a dialog that looks something like this:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintPreviewDlg.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pysidePrintPreviewDlg-300x211.png" alt="pysidePrintPreviewDlg" width="300" height="211" class="aligncenter size-medium wp-image-3134" /></a></p>
<p>You now know how to create both types of PySide&#8217;s print dialogs! At this point, we&#8217;re ready to move on to message boxes!</p>
<h3>PySide&#8217;s Message Boxes</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/messageboxdemo.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/messageboxdemo-300x191.png" alt="messageboxdemo" width="300" height="191" class="aligncenter size-medium wp-image-3125" /></a></p>
<p>PySide has four Message Boxes based on the QMessageBox widget: critical, information, question and warning. We&#8217;re going to take a look at all four. We&#8217;ll start out with a full code example that is a demo that can launch any of the 4 message boxes and then we&#8217;ll look at each piece as we did in the previous section. Here&#8217;s the main program&#8217;s source:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> PySide <span style="color: #ff7700;font-weight:bold;">import</span> QtCore, QtGui
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MessageBoxDemo<span style="color: black;">&#40;</span>QtGui.<span style="color: black;">QWidget</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><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>
        <span style="color: #808080; font-style: italic;"># super(DialogDemo, self).__init__()</span>
        QtGui.<span style="color: black;">QWidget</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: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MessageBox Demo&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># create buttons</span>
        criticalMsgBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Show Critical Message&quot;</span><span style="color: black;">&#41;</span>
        criticalMsgBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onShowCriticalMessage</span><span style="color: black;">&#41;</span>
&nbsp;
        infoMsgBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Show Informational Message&quot;</span><span style="color: black;">&#41;</span>
        infoMsgBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onShowInformation</span><span style="color: black;">&#41;</span>
&nbsp;
        questionMsgBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Show Question Message&quot;</span><span style="color: black;">&#41;</span>
        questionMsgBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onShowQuestion</span><span style="color: black;">&#41;</span>
&nbsp;
        warningMsgBtn = QtGui.<span style="color: black;">QPushButton</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Show Warning Message&quot;</span><span style="color: black;">&#41;</span>
        warningMsgBtn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onShowWarning</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout widgets</span>
        layout = QtGui.<span style="color: black;">QVBoxLayout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>criticalMsgBtn<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>infoMsgBtn<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>questionMsgBtn<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>warningMsgBtn<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>layout<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setGeometry</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">300</span>,<span style="color: #ff4500;">100</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> onShowCriticalMessage<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;
        Show the critical message
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Abort</span>
        flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Retry</span>
        flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Ignore</span>
&nbsp;
        result = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">critical</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;CRITICAL ERROR&quot;</span>,
                                            <span style="color: #483d8b;">&quot;You're trying Perl aren't you?&quot;</span>,
                                            flags<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Abort</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Aborted!&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Retry</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Retrying!&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Ignore</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Ignoring!&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onShowInformation<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;
        Show the information message
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">information</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Information&quot;</span>, <span style="color: #483d8b;">&quot;Python rocks!&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> onShowQuestion<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;
        Show the question message
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Yes</span> 
        flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">No</span>
        question = <span style="color: #483d8b;">&quot;Do you really want to work right now?&quot;</span>
        response = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">question</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Question&quot;</span>,
                                              question,
                                              flags<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> response == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Yes</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You're very dedicated!&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">No</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You're fired!&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You chose wisely!&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onShowWarning<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;
        Show a warning message
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Ok</span>
        flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Discard</span>
        msg = <span style="color: #483d8b;">&quot;This message will self-destruct in 30 seconds!&quot;</span>
        response = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">warning</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Warning!&quot;</span>,
                                             msg, flags<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> response == <span style="color: #ff4500;">0</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;BOOM!&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;MessageBox disarmed!&quot;</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 = QtGui.<span style="color: black;">QApplication</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    form = MessageBoxDemo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Now let&#8217;s take a look at each message box in turn. The first one we&#8217;ll look at is the critical message box. </p>
<h4>The Critical Message Box</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/criticalBox.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/criticalBox-300x131.png" alt="criticalBox" width="300" height="131" class="aligncenter size-medium wp-image-3127" /></a></p>
<p>The critical message box is launched from our demo program using the following method:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onShowCriticalMessage<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;
    Show the critical message
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Abort</span>
    flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Retry</span>
    flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Ignore</span>
&nbsp;
    result = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">critical</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;CRITICAL ERROR&quot;</span>,
                                        <span style="color: #483d8b;">&quot;You're trying Perl aren't you?&quot;</span>,
                                        flags<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Abort</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Aborted!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Retry</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Retrying!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> result == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Ignore</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Ignoring!&quot;</span></pre>
<p>All message boxes follow the same basic formula. They have a caption (&#8220;CRITICAL ERROR&#8221;), a message (&#8220;You&#8217;re trying Perl aren&#8217;t you?&#8221;) and some flags that control what buttons are shown. In this case, we show an Abort, a Retry and an Ignore button. We print out a special message to stdout that depends on which button is pressed. </p>
<h4>The Information Message Box</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/infoBox.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/infoBox.png" alt="infoBox" width="181" height="148" class="aligncenter size-full wp-image-3128" /></a></p>
<p>The information message box is probably the simplest of any of the message boxes:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onShowInformation<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;
    Show the information message
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">information</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Information&quot;</span>, <span style="color: #483d8b;">&quot;Python rocks!&quot;</span><span style="color: black;">&#41;</span></pre>
<p>As you can see, we didn&#8217;t even need to specify any buttons for this one. It added an OK button all by itself.</p>
<h4>The Question Message Box</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/questionBox.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/questionBox-300x128.png" alt="questionBox" width="300" height="128" class="aligncenter size-medium wp-image-3129" /></a></p>
<p>The question message box is pretty simple too. Here&#8217;s the code:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onShowQuestion<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;
    Show the question message
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Yes</span> 
    flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">No</span>
    question = <span style="color: #483d8b;">&quot;Do you really want to work right now?&quot;</span>
    response = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">question</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Question&quot;</span>,
                                          question,
                                          flags<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> response == QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">Yes</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You're very dedicated!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">No</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You're fired!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You chose wisely!&quot;</span></pre>
<p>For this message box, we needed two buttons: yes and no. We also check which button the user pressed and print out special messages based on their choice. You&#8217;ll note that there is no way to get the last print statement to execute.</p>
<h4>The Warning Message Box</h4>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/warningBox.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/warningBox-300x113.png" alt="warningBox" width="300" height="113" class="aligncenter size-medium wp-image-3130" /></a></p>
<p>The warning message box is also quite simple and very similar to the question box.</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onShowWarning<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;
    Show a warning message
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    flags = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Ok</span>
    flags |= QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">StandardButton</span>.<span style="color: black;">Discard</span>
    msg = <span style="color: #483d8b;">&quot;This message will self-destruct in 30 seconds!&quot;</span>
    response = QtGui.<span style="color: black;">QMessageBox</span>.<span style="color: black;">warning</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Warning!&quot;</span>,
                                         msg, flags<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> response == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;BOOM!&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;MessageBox disarmed!&quot;</span></pre>
<p>Here we have two buttons: an OK button and a discard button. Again we check to see which button is pressed and act accordingly.</p>
<h3>Wrapping Up</h3>
<p>
<p>Now you know how to create the various standard dialogs and message boxes that PySide provides. These are quite handy when you need to alert the user to something that is amiss in your program or to acquire small bits of information. Now go forth and start using your new-found knowledge wisely!</p>
<h3>References</h3>
<p>
<ul>
<li>Zetcode&#8217;s PySide <a href="http://zetcode.com/gui/pysidetutorial/dialogs/" target="_blank">tutorial on dialogs</a></li>
<li>A PySide <a href="http://qt.gitorious.org/pyside/pyside-examples/blobs/f6a7054d4e730787ca86a473023aedc4af4ae649/examples/dialogs/standarddialogs.py" target="_blank">repo on git</a> that shows standard dialog and message box examples</li>
<li>StackOverflow &#8211; <a href="http://stackoverflow.com/questions/8193920/print-a-text-through-a-printer-using-pyqt4" target="_blank">Print a text through a printer using PyQt4</a></li>
</ul>
<h3>Download the Source</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_dialogs.zip">pyside_dialogs.zip</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/SzvESyRTedY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/16/pyside-standard-dialogs-and-message-boxes/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/16/pyside-standard-dialogs-and-message-boxes/</feedburner:origLink></item>
		<item>
		<title>Python Brochure Project Preview</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/RK9d81Hjllo/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/16/python-brochure-project-preview/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 13:20:58 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Advocacy]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3064</guid>
		<description><![CDATA[I had heard about the Python Brochure Project last year but was under the impression that it was for corporations at that time as it seemed that you had to purchase them. It looked really good, but since I wasn&#8217;t a corporation and couldn&#8217;t buy them in bulk, I didn&#8217;t give it too much thought. [...]]]></description>
				<content:encoded><![CDATA[<p>I had heard about the Python Brochure Project last year but was under the impression that it was for corporations at that time as it seemed that you had to purchase them. It looked really good, but since I wasn&#8217;t a corporation and couldn&#8217;t buy them in bulk, I didn&#8217;t give it too much thought. However the Python Software Foundation has now released a <a href="http://brochure.getpython.info/learn-more" target="_blank">preview </a>of the brochure that&#8217;s worth a look. The brochure is for the marketing of Python, primarily for non-technical people (such as managers). If you&#8217;ve been struggling to get Python adopted at your place of work, this brochure might help as it showcases how Python is used in business in various fields from industry and science to education and government.</p>
<p>You can download it from their website and take a look. I thought it was pretty slick. You can even submit your own case studies to be included or help sponsor the brochure. Go check it out and spread the word!</p>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/RK9d81Hjllo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/16/python-brochure-project-preview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/16/python-brochure-project-preview/</feedburner:origLink></item>
		<item>
		<title>PySide: Connecting Multiple Widgets to the Same Slot</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/oYtghUeY82U/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 21:54:00 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[GUI Toolkits]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[PyQT]]></category>
		<category><![CDATA[PySide]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3078</guid>
		<description><![CDATA[As I learn PyQt and PySide, I am writing some tutorials to help my fellow travelers. Today we&#8217;ll be looking at how to connect multiple widgets to the same slot. In other words, we&#8217;ll be binding the widgets signals (basically events) to slots (i.e. callables like functions, methods) which are better known as &#8220;event handlers&#8221;. [...]]]></description>
				<content:encoded><![CDATA[<p>As I learn PyQt and PySide, I am writing some tutorials to help my fellow travelers. Today we&#8217;ll be looking at how to connect multiple widgets to the same slot. In other words, we&#8217;ll be binding the widgets signals (basically events) to slots (i.e. callables like functions, methods) which are better known as &#8220;event handlers&#8221;. Yes, I know in PySide land that you don&#8217;t call it that. It&#8217;s a SLOT, not an event handler. But I digress. Anyway, there are two ways to approach this. One is to use functools and its partial class to pass parameters or to use PySide&#8217;s introspection abilities to grab that information from the widget that did the calling. There&#8217;s actually at least one other method that is very similar to functools.partial and that is to use the infamous anonymous function utility known as lambda. Let&#8217;s take a look at how all these methods can be done!<span id="more-3078"></span></p>
<h3>Getting Started</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_multipleButtons.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_multipleButtons-300x229.png" alt="pyside_multipleButtons" width="300" height="229" class="aligncenter size-medium wp-image-3091" /></a></p>
<p>If you don&#8217;t have PySide yet, go out and get it. If you want to try this with PyQt, make sure your version of PyQt is 4.3 or greater as the first example of partial in the following sample application won&#8217;t work in previous versions of PyQt. You can get PySide <a href="http://qt-project.org/wiki/Category:LanguageBindings::PySide" target="_blank">here</a>. Once you&#8217;re all set up and ready to rock, go ahead and read the following code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> partial
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QLabel, QWidget
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QPushButton, QVBoxLayout
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MultiButtonDemo<span style="color: black;">&#40;</span>QWidget<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><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>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>MultiButtonDemo, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        layout = QVBoxLayout<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span> = QLabel<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;You haven't pressed a button!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># use a normal signal / slot mechanic</span>
        button1 = QPushButton<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;One&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>button1, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;clicked()&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">one</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># now let's use partial functions</span>
        button2 = QPushButton<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Two&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>button2, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;clicked()&quot;</span><span style="color: black;">&#41;</span>,
                     partial<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onButton</span>, <span style="color: #483d8b;">&quot;Two&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        button3 = QPushButton<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Three&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">btn3Callback</span> = partial<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">onButton</span>, <span style="color: #483d8b;">&quot;Three&quot;</span><span style="color: black;">&#41;</span>
        button3.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">btn3Callback</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># now let's try using a lambda function</span>
        button4 = QPushButton<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Four&quot;</span><span style="color: black;">&#41;</span>
        button4.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> name=<span style="color: #483d8b;">&quot;Four&quot;</span>: <span style="color: #008000;">self</span>.<span style="color: black;">onButton</span><span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">label</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>button1<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>button2<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>button3<span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>button4<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>layout<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PySide Demo&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> one<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;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;You pressed One!&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> onButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, lbl<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Change the label to the text (lbl) passed in&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;You pressed %s!&quot;</span> <span style="color: #66cc66;">%</span> lbl<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 = QApplication<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    form = MultiButtonDemo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Let&#8217;s spend a little time breaking this down. We subclass QWidget to create our window. Then we use a QVBoxLayout to layout our widgets in a vertical row. Next we create a label and four buttons. The first button we hook up to an event handler is done in the normal way. In the <strong>one</strong> handler, it just updates the label to say that &#8220;You Pressed One!&#8221;. You could do this for the other 3 buttons, but that&#8217;s not the point of this article. Instead, we use Python&#8217;s <strong>functools</strong> library which has a very handy <strong>partial</strong> class. The partial class allows the developer to create a callable that you can give to your PySide signal. The first example creates a partial callable that is inaccessible to anything else. The 2nd example (button 3) of partial is assigned to a class variable, <strong>self.btn3Callback</strong> which you can then give to your signal. The last example (button 4) shows you how to do the same thing as partial, but by using Python&#8217;s lambda instead. This last method is especially popular with the Tkinter GUI toolkit.</p>
<p>Now we&#8217;re ready to find out how to make PySide itself tell us which button signaled the slot.</p>
<h3>How to Make PySide Tell All</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_multipleButtons2.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_multipleButtons2-300x187.png" alt="pyside_multipleButtons2" width="300" height="187" class="aligncenter size-medium wp-image-3092" /></a></p>
<p>We&#8217;re actually going to refactor the code a bit to make the code shorter and to show you another way to layout your widgets. This time around, we&#8217;re also going to have all the buttons connect to just one slot (i.e. event handler / callable). Here&#8217;s the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QLabel, QWidget
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QPushButton, QVBoxLayout
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MultiButtonDemo<span style="color: black;">&#40;</span>QWidget<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><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>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>MultiButtonDemo, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        layout = QVBoxLayout<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">label</span> = QLabel<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;You haven't pressed a button!&quot;</span><span style="color: black;">&#41;</span>
        layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">label</span><span style="color: black;">&#41;</span>
        labels = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;One&quot;</span>, <span style="color: #483d8b;">&quot;Two&quot;</span>, <span style="color: #483d8b;">&quot;Three&quot;</span>, <span style="color: #483d8b;">&quot;Four&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> label <span style="color: #ff7700;font-weight:bold;">in</span> labels:
            btn = QPushButton<span style="color: black;">&#40;</span>label<span style="color: black;">&#41;</span>
            btn.<span style="color: black;">clicked</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">clicked</span><span style="color: black;">&#41;</span>
            layout.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>btn<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>layout<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PySide Signals / Slots Demo&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> clicked<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;
        Change label based on what button was pressed
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        button = <span style="color: #008000;">self</span>.<span style="color: black;">sender</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>button, QPushButton<span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">label</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;You pressed %s!&quot;</span> <span style="color: #66cc66;">%</span> button.<span style="color: black;">text</span><span style="color: black;">&#40;</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 = QApplication<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    form = MultiButtonDemo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Okay, the imports are a little shorter now as we no longer need functools. Down in the <strong>__init__</strong> we create a list of button labels and then loop over them to create the four buttons. In the loop, we also connect the button to the callable (or the signal to the slot) and add the widget to the layout manager. According to Mark Summerfield, author of <a href="http://www.amazon.com/gp/product/0132354187/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0132354187&#038;linkCode=as2&#038;tag=thmovsthpy-20" target="_blank">Rapid GUI Programming with Python and PyQt</a> says in his book that the callable (slot) should be named the same as the signal as that is the convention of PyQt. My guess is that is the convention in PySide too, so we&#8217;re sticking with it.</p>
<p>In our <strong>clicked</strong> method/signal/callable/whatever we use <strong>self.sender()</strong> to grab the button object from whatever calls the method. Then, just to be safe, we make sure that the sender is an instance of QPushButton and if it is, then we grab the label of the button via its <strong>text()</strong> call and set the label appropriately. That&#8217;s it! Short and sweet!</p>
<h3>Wrapping Up</h3>
<p>
<p>Now you know several ways to pass arguments to a slot/handler. You have also learned how to ask PySide which widget called the callable. Now you can get out there and code up some cool stuff on your own.</p>
<h3>Additional Reading</h3>
<p>
<ul>
<li>Python&#8217;s documentation on <a href="http://docs.python.org/2/library/functools.html#functools.partial" target="_blank">functools.partial</a></li>
<li><a href="http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/" target="_blank">The Python Lambda</a></li>
<li>wxPython: <a href="http://www.blog.pythonlibrary.org/2011/09/20/wxpython-binding-multiple-widgets-to-the-same-handler/" target="_blank">Binding Multiple Widgets to the Same Handler</a></li>
<li>wxPython wiki &#8211; <a href="http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks" target="_blank">Passing Arguments to Callbacks</a></li>
</ul>
<h3>Download the Source</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_passingArgstoSlots.zip">pyside_passingArgstoSlots.zip</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/oYtghUeY82U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/</feedburner:origLink></item>
		<item>
		<title>PySide: Creating a Currency Converter</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/SBNVYrb_3mA/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/09/pyside-creating-a-currency-converter/#comments</comments>
		<pubDate>Tue, 09 Apr 2013 21:53:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Python GUI Toolkits]]></category>
		<category><![CDATA[PyQT]]></category>
		<category><![CDATA[PySide]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3054</guid>
		<description><![CDATA[I am currently reading through Mark Summerfield&#8217;s book on PyQt, Rapid GUI Programming with Python and Qt and thought it would be fun to take some of the example applications in it and convert them to PySide. So I&#8217;ll be creating a series of articles where I&#8217;ll show the original PyQt examples from the book [...]]]></description>
				<content:encoded><![CDATA[<p>I am currently reading through Mark Summerfield&#8217;s book on PyQt, <a href="http://www.amazon.com/gp/product/0132354187/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0132354187&#038;linkCode=as2&#038;tag=thmovsthpy-20" target="_blank">Rapid GUI Programming with Python and Qt</a> and thought it would be fun to take some of the example applications in it and convert them to PySide. So I&#8217;ll be creating a series of articles where I&#8217;ll show the original PyQt examples from the book and then convert them to PySide and probably add something of my own to the code. The book doesn&#8217;t really get in Qt GUI coding until chapter 4 where the author creates a fun little currency converter. Come along and enjoy the fun!<span id="more-3054"></span></p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyqt_currency.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyqt_currency-300x95.png" alt="pyqt_currency" width="300" height="95" class="aligncenter size-medium wp-image-3068" /></a></p>
<p>Here&#8217;s the mostly original code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QComboBox, QDialog, QDoubleSpinBox, QLabel
<span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QGridLayout
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> CurrencyDlg<span style="color: black;">&#40;</span>QDialog<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: #008000;">None</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>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CurrencyDlg, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>parent<span style="color: black;">&#41;</span>
&nbsp;
        date = <span style="color: #008000;">self</span>.<span style="color: black;">getdata</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        rates = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rates</span>.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        dateLabel = QLabel<span style="color: black;">&#40;</span>date<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span> = QComboBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>.<span style="color: black;">addItems</span><span style="color: black;">&#40;</span>rates<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span> = QDoubleSpinBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">setRange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.01</span>, <span style="color: #ff4500;">10000000.00</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">setValue</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.00</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span> = QComboBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>.<span style="color: black;">addItems</span><span style="color: black;">&#40;</span>rates<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span> = QLabel<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;1.00&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout the controls</span>
        grid = QGridLayout<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>dateLabel, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>grid<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># set up the event handlers</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;currentIndexChanged(int)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;currentIndexChanged(int)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;valueChanged(double)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Currency&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> getdata<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;
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">rates</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        url = <span style="color: #483d8b;">&quot;http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            date = <span style="color: #008000;">None</span>
            fh = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fh:
                line = line.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line <span style="color: #ff7700;font-weight:bold;">or</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;#&quot;</span>, <span style="color: #483d8b;">&quot;Closing &quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
                    <span style="color: #ff7700;font-weight:bold;">continue</span>
                fields = line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;,&quot;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Date &quot;</span><span style="color: black;">&#41;</span>:
                    date = fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">try</span>:
                        value = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                        <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span><span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = value
                    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
                        <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Exchange Rates Date: &quot;</span> + date
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Failed to download: <span style="color: #000099; font-weight: bold;">\n</span>%s&quot;</span> <span style="color: #66cc66;">%</span> e
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> updateUi<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;
        Update the user interface
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        to = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>.<span style="color: black;">currentText</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        frum = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>.<span style="color: black;">currentText</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        amount = <span style="color: black;">&#40;</span> <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span>frum<span style="color: black;">&#93;</span> / <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span>to<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> 
        amount <span style="color: #66cc66;">*</span>= <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">value</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%0.2f&quot;</span> <span style="color: #66cc66;">%</span> amount<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 = QApplication<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span>
    form = CurrencyDlg<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>If you really want to know how this code works, I recommend getting the aforementioned book or by studying the code. It is Python after all.</p>
<h3>Porting to PySide</h3>
<p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_currency.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyside_currency-300x94.png" alt="pyside_currency" width="300" height="94" class="aligncenter size-medium wp-image-3069" /></a></p>
<p>Now we need to &#8220;port&#8221; this to <a href="http://qt-project.org/wiki/Category:LanguageBindings::PySide" target="_blank">PySide</a>, the LGPL version of the QT bindings for Python. Fortunately, in this example it is beyond easy. All you have to do is change the following imports</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QComboBox, QDialog, QDoubleSpinBox, QLabel
<span style="color: #ff7700;font-weight:bold;">from</span> PyQt4.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QGridLayout</pre>
<p>to</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QComboBox, QDialog, QDoubleSpinBox
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QGridLayout, QLabel</pre>
<p>If you do that and save it, you&#8217;ll be done porting and the code will now work in PySide land. However, I think we need to go a bit further and make this code a little smarter. You&#8217;ll notice in the original that every time you run it, the application will go online and download the exchange rate data. That seems to be a little bit of overkill, so let&#8217;s add a check to see if it&#8217;s already downloaded and less than a day old. Only if it&#8217;s older than a day will we want to download a new copy. And instead of using urllib2, let&#8217;s use the new-fangled <a href="http://docs.python-requests.org/en/latest/" target="_blank">requests</a> library instead.</p>
<p>Here&#8217;s the new code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> PySide
<span style="color: #ff7700;font-weight:bold;">import</span> requests
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtCore</span> <span style="color: #ff7700;font-weight:bold;">import</span> SIGNAL
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QComboBox, QDialog, QDoubleSpinBox
<span style="color: #ff7700;font-weight:bold;">from</span> PySide.<span style="color: black;">QtGui</span> <span style="color: #ff7700;font-weight:bold;">import</span> QApplication, QGridLayout, QLabel
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> CurrencyDlg<span style="color: black;">&#40;</span>QDialog<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: #008000;">None</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>
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CurrencyDlg, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>parent<span style="color: black;">&#41;</span>
&nbsp;
        date = <span style="color: #008000;">self</span>.<span style="color: black;">getdata</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        rates = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rates</span>.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        dateLabel = QLabel<span style="color: black;">&#40;</span>date<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span> = QComboBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>.<span style="color: black;">addItems</span><span style="color: black;">&#40;</span>rates<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span> = QDoubleSpinBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">setRange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.01</span>, <span style="color: #ff4500;">10000000.00</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">setValue</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1.00</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span> = QComboBox<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>.<span style="color: black;">addItems</span><span style="color: black;">&#40;</span>rates<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span> = QLabel<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;1.00&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># layout the controls</span>
        grid = QGridLayout<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span>dateLabel, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        grid.<span style="color: black;">addWidget</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">setLayout</span><span style="color: black;">&#40;</span>grid<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># set up the event handlers</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;currentIndexChanged(int)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;currentIndexChanged(int)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;valueChanged(double)&quot;</span><span style="color: black;">&#41;</span>,
                     <span style="color: #008000;">self</span>.<span style="color: black;">updateUi</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">setWindowTitle</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Currency - by PySide %s&quot;</span> <span style="color: #66cc66;">%</span> PySide.__version__<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> downloadFile<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, rate_file<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Download the file
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        url = <span style="color: #483d8b;">&quot;http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv&quot;</span>
        r = requests.<span style="color: black;">get</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            with <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>rate_file, <span style="color: #483d8b;">&quot;wb&quot;</span><span style="color: black;">&#41;</span> as f_handler:
                f_handler.<span style="color: black;">write</span><span style="color: black;">&#40;</span>r.<span style="color: black;">content</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ERROR: Unable to download file to %s&quot;</span> <span style="color: #66cc66;">%</span> rate_file
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> getdata<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;
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        base_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        rate_file = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>base_path, <span style="color: #483d8b;">&quot;exchange_eng.csv&quot;</span><span style="color: black;">&#41;</span>
        today = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">rates</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>rate_file<span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">downloadFile</span><span style="color: black;">&#40;</span>rate_file<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #808080; font-style: italic;"># get last modified date:</span>
            ts = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getmtime</span><span style="color: black;">&#40;</span>rate_file<span style="color: black;">&#41;</span>
            last_modified = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">fromtimestamp</span><span style="color: black;">&#40;</span>ts<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> today.<span style="color: black;">day</span> <span style="color: #66cc66;">!</span>= last_modified.<span style="color: black;">day</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">downloadFile</span><span style="color: black;">&#40;</span>rate_file<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            date = <span style="color: #008000;">None</span>
            with <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>rate_file<span style="color: black;">&#41;</span> as fh:
                <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> fh:
                    result = <span style="color: #008000;">self</span>.<span style="color: black;">processLine</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> result <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span>:
                        date = result
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Exchange Rates Date: &quot;</span> + date
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Failed to download: <span style="color: #000099; font-weight: bold;">\n</span>%s&quot;</span> <span style="color: #66cc66;">%</span> e
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> processLine<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, line<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Processes each line and updates the &quot;</span>rates<span style="color: #483d8b;">&quot; dictionary
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        line = line.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line <span style="color: #ff7700;font-weight:bold;">or</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;#&quot;</span>, <span style="color: #483d8b;">&quot;Closing &quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span>
        fields = line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;,&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Date &quot;</span><span style="color: black;">&#41;</span>:
            date = fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> date
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                value = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span><span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>fields<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = value
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> updateUi<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;
        Update the user interface
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        to = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">toComboBox</span>.<span style="color: black;">currentText</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        frum = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">fromComboBox</span>.<span style="color: black;">currentText</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        amount = <span style="color: black;">&#40;</span> <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span>frum<span style="color: black;">&#93;</span> / <span style="color: #008000;">self</span>.<span style="color: black;">rates</span><span style="color: black;">&#91;</span>to<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span> 
        amount <span style="color: #66cc66;">*</span>= <span style="color: #008000;">self</span>.<span style="color: black;">fromSpinBox</span>.<span style="color: black;">value</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">toLabel</span>.<span style="color: black;">setText</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%0.2f&quot;</span> <span style="color: #66cc66;">%</span> amount<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 = QApplication<span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span>
    form = CurrencyDlg<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    form.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">exec_</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Okay, let&#8217;s spend a little time looking at the code. We won&#8217;t explain everything, but we&#8217;ll go over what was changed. First off, we imported some more modules: <strong>datetime</strong>, <strong>os</strong>, <strong>requests </strong>and <strong>sys</strong>. We also imported PySide itself so we could easily add its version information to the dialog&#8217;s title bar. Next we added a couple of new methods: <strong>downloadFile</strong> and <strong>processLine</strong>. But first we need to look at what was changed in <strong>getdata</strong>. Here we grab the path that the script is running in and use that to create a fully qualified path to the rates file. Then we check if it already exists. If it doesn&#8217;t exist, we call the <strong>downloadFile</strong> method which uses the <strong>requests</strong> library to download the file. We&#8217;re also using Python&#8217;s <strong>with</strong> context manager construct which will automatically close the file when we&#8217;re done writing to it.</p>
<p>If the file DOES exist, then we fall to the else part of the statement in which we check if the file&#8217;s last modified date is older than today&#8217;s date. If it is, then we download the file and overwrite the original. Then we move on to processing the file. To do the processing, we move most of the code into a separate method called <strong>processLine</strong>. The primary reason for doing so is that if we don&#8217;t, we end up with a very complicated nested structure that can be hard to follow. We also add a check to see if the date has been returned from processLine and if it has, we set the date variable. The rest of the code stayed the same.</p>
<h3>Wrapping Up</h3>
<p>
<p>At this point, you should know how to create a very simple PySide application that can actually do something useful. Not only that, but you also have a very general idea of how to port your PyQt code to PySide. You have also been exposed to two different ways of downloading files from off the web. I hope this has opened your eyes to the power of Python and the fun of programming.</p>
<p><em>Note: The code in this article was tested on Windows 7 Professional with Python 2.6.6, PySide 1.2.2 and PyQt4</em></p>
<h3>Related Links</h3>
<p>
<ul>
<li>Python 101: <a href="http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/" target="_blank">How to Download a File</a></li>
<li>The Official PySide <a href="http://qt-project.org/wiki/Category:LanguageBindings::PySide" target="_blank">website</a></li>
<li>The Official PyQt <a href="http://www.riverbankcomputing.com/software/pyqt/intro" target="_blank">website</a></li>
<li>The <a href="http://docs.python-requests.org/en/latest/" target="_blank">requests package</a></li>
<li><strong>UPDATE (04/10/2013)</strong> &#8211; I noticed someone else decided to do this same tutorial as a <a href="http://www.youtube.com/watch?v=8D_aEYiBU2c&#038;list=PLA955A8F9A95378CE&#038;index=4" target="_blank">video tutorial</a></li>
</ul>
<h3>Download the Source</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/pyqt-side-currency.zip">pyqt-side-currency.zip</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/SBNVYrb_3mA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/09/pyside-creating-a-currency-converter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/09/pyside-creating-a-currency-converter/</feedburner:origLink></item>
		<item>
		<title>eBook Review: Treading on Python Volume 2: Intermediate Python</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/sEoMtli0hMo/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/03/ebook-review-treading-on-python-volume-2-intermediate-python/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 21:50:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3055</guid>
		<description><![CDATA[Last week, Matt Harrison sent me a copy of his latest Python eBook entitled Treading on Python Vol 2: Intermediate Python. I was intrigued since I rarely get to read Intermediate level Python books. In fact, I would say that some of the stuff that the author talks about goes into the advanced level. Anyway, [...]]]></description>
				<content:encoded><![CDATA[<p>Last week, <a href="http://hairysun.com/" target="_blank">Matt Harrison</a> sent me a copy of his latest Python eBook entitled <strong><a href="http://www.amazon.com/gp/product/B00BT95CWM/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B00BT95CWM&#038;linkCode=as2&#038;tag=thmovsthpy-20" target="_blank">Treading on Python Vol 2: Intermediate Python</a></strong>. I was intrigued since I rarely get to read Intermediate level Python books. In fact, I would say that some of the stuff that the author talks about goes into the advanced level. Anyway, I thought it was a pretty good little book and if you have a moment, I&#8217;ll tell you why.</p>
<h3>Quick Review</h3>
<p>
<ul>
<li><strong>Why I picked it up:</strong> As I mentioned, the fact that it was an intermediate level book made this a must-read for me.</li>
<li><strong>Why I finished it:</strong> Because the book proved to be very interesting. </li>
<li><strong>I&#8217;d give it to:</strong> Python programmers who have a good understanding of the basics but are looking to grow in their Python skills. </li>
</ul>
<p>Now if you have a few more minutes, you can read my full review after the jump!<span id="more-3055"></span></p>
<h3>Book Formats</h3>
<p>
<p>This book is available on Amazon as a Kindle book only at the time of writing as far as I can tell. The copy I received was in epub format though, so there may be another version available somewhere&#8230;</p>
<h3>Full Review</h3>
<p>
<p>Matt Harrison&#8217;s latest book is his best yet. I found the writing to be almost error free with only a smattering of typos here and there, mostly in the 2nd half of the book. The book is split up into 3 sections (not including the Introduction). Harrison doesn&#8217;t spend time introducing us to Python; instead he assumes you already know it and dives off into the deep end with functional constructs. Thus begins the first section in which he covers lambda, map, reduce, filter, recursion, list, set and dict comprehensions, and finally, the operator module. He used a lot of the topics that followed the lambda to illustrate advanced uses of the lambda construct. I thought it was intriguing and ended up learning some new tricks that I hope to implement in my own code soon.</p>
<p>The 2nd section is devoted to <strong>Iteration and Generators</strong>. In it, you will learn the difference between iterables and iterators, how to construct a normal generator and an object generator and the author also gives tips about when to use a generator versus a list. He also shows some real life examples of generators and iterators in the Python core. He does this a little in the first section as well.</p>
<p>The 3rd section is all about Functions, Closures and Decorators with a heavy emphasis on the latter. I think this section is a shorter, updated version of his <a href="http://www.amazon.com/gp/product/B006ZHJSIM/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B006ZHJSIM&#038;linkCode=as2&#038;tag=thmovsthpy-20" target="_blank">book on Decorators</a> actually as some of the examples looked kind of familiar. Regardless, it was very illuminating. I will admit that the &#8220;Alternate Decorator Implementations&#8221; section at the end of the book was rather confusing though.</p>
<p>In the end, I think this book is well worth the cost of owning it. You&#8217;ll almost certainly pick up something new just in the first section. If you don&#8217;t know much about generators, iterators or decorators in Python, then this book will help you figure it out and hopefully give you ideas of how you could use them in your code. I know I learned a few things (and probably re-learned a few others!).</p>
<table>
<tr>
<td><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/treading_on_python_vol2.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/treading_on_python_vol2.png" alt="treading_on_python_vol2" width="207" height="278" class="aligncenter size-full wp-image-3056" /></a></td>
<td>
<h3>Treading on Python Volume 2: Intermediate Python</h3>
<p><p>Matt Harrison</p>
<p><strong><a href="http://www.amazon.com/gp/product/B00BT95CWM/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B00BT95CWM&#038;linkCode=as2&#038;tag=thmovsthpy-20">Amazon</a></strong></p>
</td>
</tr>
</table>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/sEoMtli0hMo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/03/ebook-review-treading-on-python-volume-2-intermediate-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/03/ebook-review-treading-on-python-volume-2-intermediate-python/</feedburner:origLink></item>
		<item>
		<title>eBook Review: Learn Python Quickly</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/uEUkVXuZtqs/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/04/01/ebook-review-learn-python-quickly/#comments</comments>
		<pubDate>Mon, 01 Apr 2013 20:04:36 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Books]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3026</guid>
		<description><![CDATA[A couple months ago, I was contacted by John Rowland. He is the author of the brand new eBook Learn Python Quickly and he was contacting me about using one of my blog articles as the basis for one of his examples in his book. This one to be exact. Anyway, I told him I [...]]]></description>
				<content:encoded><![CDATA[<p>A couple months ago, I was contacted by John Rowland. He is the author of the brand new eBook <a href="http://www.amazon.com/gp/product/B00BU3LIOI/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B00BU3LIOI&#038;linkCode=as2&#038;tag=thmovsthpy-20" target="_blank">Learn Python Quickly</a> and he was contacting me about using one of my blog articles as the basis for one of his examples in his book. This <a href="http://www.blog.pythonlibrary.org/2012/07/26/tkinter-how-to-show-hide-a-window/" target="_blank">one</a> to be exact. Anyway, I told him I would review his book when it came out. Anyway, the book came out a couple weeks ago and here is my review. <strong>Note: I managed to snag a copy of this when it was free, which was the second day it was on sale, I think.</strong></p>
<h3>Quick Review</h3>
<p>
<ul>
<li><strong>Why I picked it up:</strong> I picked this one up because first I was intrigued that someone wanted to use one of my articles in their book and secondly, I got a sneak peak of a couple of the chapters and enjoyed the author&#8217;s writing style. Also, picking it up for free helped!</li>
<li><strong>Why I finished it:</strong> Because I wanted to review the book. I only read the main text though and only skimmed the rather extensive glossary / appendices. </li>
<li><strong>I&#8217;d give it to:</strong> New programmers, especially if they already had a little programming already, like a 101 class.  </li>
</ul>
<p>You can read my full review after the jump!</p>
<p><span id="more-3026"></span></p>
<h3>Book Formats</h3>
<p>
<p>This book is available on Amazon as a Kindle book only at the time of writing.</p>
<h3>Full Review</h3>
<p>
<p><strong>Learn Python Quickly</strong> teaches Python 3. It covers all the topics that you would expect and a couple that you wouldn&#8217;t. I didn&#8217;t expect it to just cover installing Python, especially only on Windows. I was also surprised that a book as short as this one would attempt to jump into GUI programming with Tkinter for the last 3 chapters, but it did. The normal stuff is covered too of course (not necessarily in this order): numbers, strings, loops and if statements, lists and dictionaries (although not much on tuples), functions and classes, file I/O, exception handling, date manipulation and a chapter on spreadsheets and databases. </p>
<p>The spreadsheet and database chapter (number 8) focuses on the csv module for spreadsheets with a mention of the <a href="https://pypi.python.org/pypi/xlwt" target="_blank">xlwt</a> package but not <a href="https://pypi.python.org/pypi/xlrd" target="_blank">xlrd</a>. I thought this chapter might talk about PyWin32 and how to access Microsoft Excel using PyWin32&#8242;s COM modules, but it does not. The database section is a quick show-and-tell of <a href="http://code.google.com/p/pyodbc/" target="_blank">pyodbc</a>. He doesn&#8217;t teach SQL, which is fine, but should be noted. </p>
<p>The book covers the development of several applications at the end of the book. Mr. Rowland has the reader create several variations of a simple Banking application in the class chapter. Then he goes on to having the reader create a couple little Tkinter applications.</p>
<p>The most unique part about this book is that it uses TONS of hyperlinks. In fact, without the glossary and appendixes, the book would be only half as long. The author links to all kinds of terms in his glossary and to some outside sources as well. The writing style is engaging and sometimes entertaining, especially when the author drops some British terminology. I found the text to be straight-forward and easy to follow. I do have to point out a couple of cons though. There aren&#8217;t many code examples and he frequently refers to code on the book&#8217;s accompanying website: <a href="http://www.learnpythonquickly.com" target="_blank">http://www.learnpythonquickly.com</a>. Most of the time, this works quite well, but when I copy the code for the Tkinter stuff into my Python IDE, my line numbers don&#8217;t match up with the author&#8217;s very well. Also, if you try to read the book using the Amazon Cloud Reader, then the code is not indented properly most of the time, although if you open the book in Kindle software, it seems to be indenting the code appropriately.</p>
<p>Otherwise, the book was quite good and I would recommend it for people who are new to the language and want to just get up and running quickly. Of course, if you&#8217;re new, then you&#8217;ll be spending a lot of time with the Python documentation as well as this book just can&#8217;t cover the entire breadth of the entire Python language. Regardless, I think this book will allow readers to pick up the basics of Python quickly and the links will help them know what to search for later when they need to learn more.</p>
<table>
<tr>
<td><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/learn_python_quickly.jpg"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/04/learn_python_quickly.jpg" alt="learn_python_quickly" width="225" height="225" class="aligncenter size-full wp-image-3045" /></a></td>
<td>
<h3>Learn Python Quickly</h3>
<p><p>John Rowland</p>
<p><strong><a href="http://www.amazon.com/gp/product/B00BU3LIOI/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B00BU3LIOI&#038;linkCode=as2&#038;tag=thmovsthpy-20">Amazon</a></strong></p>
</td>
</tr>
</table>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/uEUkVXuZtqs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/04/01/ebook-review-learn-python-quickly/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/04/01/ebook-review-learn-python-quickly/</feedburner:origLink></item>
		<item>
		<title>Reportlab – How to Create Barcodes in Your PDFs with Python</title>
		<link>http://feedproxy.google.com/~r/TheMouseVsThePython/~3/XwgH8iymR-o/</link>
		<comments>http://www.blog.pythonlibrary.org/2013/03/25/reportlab-how-to-create-barcodes-in-your-pdfs-with-python/#comments</comments>
		<pubDate>Mon, 25 Mar 2013 21:54:20 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Reportlab]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=3024</guid>
		<description><![CDATA[The Reportlab library is a great way to generate PDFs in Python. Recently, I noticed that it has the ability to do barcodes. I had heard about it being able to generate QR codes, but I hadn&#8217;t really dug under the covers to see what else it could do. In this tutorial, we&#8217;ll take a [...]]]></description>
				<content:encoded><![CDATA[<p>The Reportlab library is a great way to generate PDFs in Python. Recently, I noticed that it has the ability to do barcodes. I had heard about it being able to generate QR codes, but I hadn&#8217;t really dug under the covers to see what else it could do. In this tutorial, we&#8217;ll take a look at some of the barcodes that Reportlab can generate. If you don&#8217;t already have Reportlab, go to their <a href="http://www.reportlab.com/" target="_blank">website </a>and get it before jumping into the article.<span id="more-3024"></span></p>
<h3>Reportlab&#8217;s Barcode Library</h3>
<p>
<p>Reportlab provides for several different types of bar codes: code39 (i.e. code 3 of 9), code93, code 128, EANBC, QR, and USPS. I saw one called &#8220;fourstate&#8221; as well, but I couldn&#8217;t figure out how to get it to work. Underneath some of these types, there are sub-types such as Standard, Extended or MultiWidth. I didn&#8217;t have much luck getting the MultiWidth one to work for the code128 bar code as it kept giving me an attribute error, so we&#8217;ll just ignore that one. If you know how to do it, ping me in the comments or via my contact form and let me know. I&#8217;ll update the article if anyone can show me how to add that or the fourstate barcode.</p>
<p>Anyway, the best way to learn is to just write some code. Here&#8217;s a pretty straight-forward example:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">graphics</span>.<span style="color: black;">barcode</span> <span style="color: #ff7700;font-weight:bold;">import</span> code39, code128, code93
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">graphics</span>.<span style="color: black;">barcode</span> <span style="color: #ff7700;font-weight:bold;">import</span> eanbc, qr, usps
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">graphics</span>.<span style="color: black;">shapes</span> <span style="color: #ff7700;font-weight:bold;">import</span> Drawing 
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">lib</span>.<span style="color: black;">pagesizes</span> <span style="color: #ff7700;font-weight:bold;">import</span> letter
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">lib</span>.<span style="color: black;">units</span> <span style="color: #ff7700;font-weight:bold;">import</span> mm
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">pdfgen</span> <span style="color: #ff7700;font-weight:bold;">import</span> canvas
<span style="color: #ff7700;font-weight:bold;">from</span> reportlab.<span style="color: black;">graphics</span> <span style="color: #ff7700;font-weight:bold;">import</span> renderPDF
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> createBarCodes<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Create barcode examples and embed in a PDF
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    c = canvas.<span style="color: black;">Canvas</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;barcodes.pdf&quot;</span>, pagesize=letter<span style="color: black;">&#41;</span>
&nbsp;
    barcode_value = <span style="color: #483d8b;">&quot;1234567890&quot;</span>
&nbsp;
    barcode39 = code39.<span style="color: black;">Extended39</span><span style="color: black;">&#40;</span>barcode_value<span style="color: black;">&#41;</span>
    barcode39Std = code39.<span style="color: black;">Standard39</span><span style="color: black;">&#40;</span>barcode_value, barHeight=<span style="color: #ff4500;">20</span>, stop=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># code93 also has an Extended and MultiWidth version</span>
    barcode93 = code93.<span style="color: black;">Standard93</span><span style="color: black;">&#40;</span>barcode_value<span style="color: black;">&#41;</span>
&nbsp;
    barcode128 = code128.<span style="color: black;">Code128</span><span style="color: black;">&#40;</span>barcode_value<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># the multiwidth barcode appears to be broken </span>
    <span style="color: #808080; font-style: italic;">#barcode128Multi = code128.MultiWidthBarcode(barcode_value)</span>
&nbsp;
    barcode_usps = usps.<span style="color: black;">POSTNET</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;50158-9999&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    codes = <span style="color: black;">&#91;</span>barcode39, barcode39Std, barcode93, barcode128, barcode_usps<span style="color: black;">&#93;</span>
&nbsp;
    x = <span style="color: #ff4500;">1</span> <span style="color: #66cc66;">*</span> mm
    y = <span style="color: #ff4500;">285</span> <span style="color: #66cc66;">*</span> mm
    x1 = <span style="color: #ff4500;">6.4</span> <span style="color: #66cc66;">*</span> mm
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">code</span> <span style="color: #ff7700;font-weight:bold;">in</span> codes:
        <span style="color: #dc143c;">code</span>.<span style="color: black;">drawOn</span><span style="color: black;">&#40;</span>c, x, y<span style="color: black;">&#41;</span>
        y = y - <span style="color: #ff4500;">15</span> <span style="color: #66cc66;">*</span> mm
&nbsp;
    <span style="color: #808080; font-style: italic;"># draw the eanbc8 code</span>
    barcode_eanbc8 = eanbc.<span style="color: black;">Ean8BarcodeWidget</span><span style="color: black;">&#40;</span>barcode_value<span style="color: black;">&#41;</span>
    bounds = barcode_eanbc8.<span style="color: black;">getBounds</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    width = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    height = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    d = Drawing<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    d.<span style="color: black;">add</span><span style="color: black;">&#40;</span>barcode_eanbc8<span style="color: black;">&#41;</span>
    renderPDF.<span style="color: black;">draw</span><span style="color: black;">&#40;</span>d, c, <span style="color: #ff4500;">15</span>, <span style="color: #ff4500;">555</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># draw the eanbc13 code</span>
    barcode_eanbc13 = eanbc.<span style="color: black;">Ean13BarcodeWidget</span><span style="color: black;">&#40;</span>barcode_value<span style="color: black;">&#41;</span>
    bounds = barcode_eanbc13.<span style="color: black;">getBounds</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    width = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    height = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    d = Drawing<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    d.<span style="color: black;">add</span><span style="color: black;">&#40;</span>barcode_eanbc13<span style="color: black;">&#41;</span>
    renderPDF.<span style="color: black;">draw</span><span style="color: black;">&#40;</span>d, c, <span style="color: #ff4500;">15</span>, <span style="color: #ff4500;">465</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># draw a QR code</span>
    qr_code = qr.<span style="color: black;">QrCodeWidget</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'www.mousevspython.com'</span><span style="color: black;">&#41;</span>
    bounds = qr_code.<span style="color: black;">getBounds</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    width = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    height = bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span> - bounds<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    d = Drawing<span style="color: black;">&#40;</span><span style="color: #ff4500;">45</span>, <span style="color: #ff4500;">45</span>, transform=<span style="color: black;">&#91;</span><span style="color: #ff4500;">45</span>./width,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">45</span>./height,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    d.<span style="color: black;">add</span><span style="color: black;">&#40;</span>qr_code<span style="color: black;">&#41;</span>
    renderPDF.<span style="color: black;">draw</span><span style="color: black;">&#40;</span>d, c, <span style="color: #ff4500;">15</span>, <span style="color: #ff4500;">405</span><span style="color: black;">&#41;</span>
&nbsp;
    c.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    createBarCodes<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Let&#8217;s break this down a bit. The code39.Extended39 doesn&#8217;t really accept much beyond the value itself. On the other hand, code39.Standard39, code93.Standard93 and code128.Code128 all have basically the same API. You can change the barWidth, barHeight, turn on the start/stop symbols and add &#8220;quiet&#8221; zones. The usps bar code module provides two types of bar code: FIM and POSTNET. FIM or Facing ID Marks only encodes one letter (A-D) which I personally didn&#8217;t find it very interesting. So I just show the POSTNET version which should be pretty familiar to people in the United States as it appears on the bottom of most envelopes. POSTNET encodes the zip code!</p>
<p>The next three bar codes use a different API to draw them on the PDF that I discovered via <a href="http://stackoverflow.com/questions/13129015/generate-multiple-qr-codes-in-one-pdf-file-using-reportlab-and-django-framework" target="_blank">StackOverflow</a>. Basically you create a <strong>Drawing</strong> object of a certain size and then add the bar code to the drawing. Finally you use the <strong>renderPDF</strong> module to place the drawing on the PDF. It&#8217;s pretty convoluted, but it works pretty well. The EANBC codes are ones you&#8217;ll see on some manufactured products, such as tissue boxes. </p>
<p>If you&#8217;d like to see the result of the code above, you can download the PDF <a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/03/barcodes.pdf">here</a>.</p>
<h3>Wrapping Up</h3>
<p>
<p>At this point you should be able to go forth and create your own bar codes in your PDFs. Reportlab is pretty handy and I hope you&#8217;ll find this additional tool helpful in your endeavors. </p>
<h3>Additional Reading</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/" target="_blank">A step by step Reportlab tutoral</a></li>
<li>Reportlab: <a href="http://www.blog.pythonlibrary.org/2012/06/27/reportlab-mixing-fixed-content-and-flowables/" target="_blank">Mixing Fixed Content and Flowables</a></li>
<li>Reportlab Tables – <a href="http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/" target="_blank">Creating Tables in PDFs with Python</a></li>
<li><a href="http://www.blog.pythonlibrary.org/2012/05/18/creating-qr-codes-with-python/" target="_blank">Creating QR Codes with Python</a></li>
<li>StackOverflow <a href="http://stackoverflow.com/questions/2179269/python-barcode-generation-library" target="_blank">question on Python barcode generation</a></li>
<li>StackOverflow <a href="http://stackoverflow.com/questions/13129015/generate-multiple-qr-codes-in-one-pdf-file-using-reportlab-and-django-framework" target="_blank">question on reportlab, QR codes and django</a></li>
</ul>
<h3>Get the Source!</h3>
<p>
<ul>
<li><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2013/03/barcodes.tar">barcodes.tar</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheMouseVsThePython/~4/XwgH8iymR-o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2013/03/25/reportlab-how-to-create-barcodes-in-your-pdfs-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.blog.pythonlibrary.org/2013/03/25/reportlab-how-to-create-barcodes-in-your-pdfs-with-python/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: www.blog.pythonlibrary.org @ 2013-06-04 13:53:26 by W3 Total Cache -->
