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

<channel>
	<title>learning python &#187; PyQt</title>
	<atom:link href="http://www.learningpython.com/category/python/pyqt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningpython.com</link>
	<description>one man's journey into python...</description>
	<lastBuildDate>Mon, 26 Apr 2010 01:21:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An Introduction to PyQt: creating GUIs with Python&#8217;s QT bindings</title>
		<link>http://www.learningpython.com/2008/09/20/an-introduction-to-pyqt/</link>
		<comments>http://www.learningpython.com/2008/09/20/an-introduction-to-pyqt/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 19:21:46 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[PyQt]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[gui]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=88</guid>
		<description><![CDATA[
			
				
			
		
By: Mark Mruss
Note: This article was first published the December 2007 issue of Python Magazine
While the command line will never cease to be useful, nothing will impress your friends more than your latest python masterpiece wrapped up in a slick cross-platform Graphical User Interface (GUI). This tutorial will show you how to create a simple [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2008%2F09%2F20%2Fan-introduction-to-pyqt%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2008%2F09%2F20%2Fan-introduction-to-pyqt%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>By: Mark Mruss</strong></p>
<p><strong>Note:</strong> This article was first published the December 2007 issue of <a href="http://www.pythonmagazine.com">Python Magazine</a></p>
<p>While the command line will never cease to be useful, nothing will impress your friends more than your latest python masterpiece wrapped up in a slick cross-platform Graphical User Interface (GUI). This tutorial will show you how to create a simple GUI in Python using PyQt4.</p>
<ol>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#InstallingPyQt4">Installing PyQt4</a></li>
<li><a href="#FirstApplication">Your First PyQt4 Application</a></li>
<li><a href="#TheMainWindow">The Main Window</a></li>
<li><a href="#AddingSomeWidgets">Adding Some Widgets</a></li>
<li><a href="#SignalHandling">Signal Handling</a></li>
<li><a href="#DisplayingAMessage">Displaying a Message</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ol>
<p><span id="more-88"></span></p>
<h2><a name="Introduction">Introduction</a></h2>
<p>While writing console applications and modules using Python can be very enjoyable, I think that almost everyone learning Python eventually wants to do one thing: create a application with a full Graphical User Interface (GUI). Fortunately for Python users, there are a few options available to achieve this. One of the more interesting options is PyQt4, Python bindings for the fourth version of the famous cross platform application development API Qt.</p>
<p>Qt, owned by Trolltech software, is probably most famous as the foundation for the KDE window manager on Linux. PyQt4, and PyQt version 3, were created and are maintained by Riverbank software.  Qt and PyQt4 are both open source and free for open source applications, but if you wish to develop commercial applications you will need to purchase the commercial versions of both Qt and PyQt4.</p>
<p>One of the best features of Qt is that it is a cross platform library which means that it gives you easy access to the three main desktop environments: Linux, Windows, and OS X. This is an important requirement for me because when I&#8217;m developing a small open source application I want it available on as many platforms as possible. The nice thing about Qt’s latest version, Qt4, is that it also gives your GUI a native look and feel on the different operating systems.</p>
<p>This article will create a simple application to introduce you to the basic features of PyQt4. The application will consist of a single window that does the following: accepts user input, responds to a button click, and displays a message using a pop-up dialog.</p>
<h2><a name="InstallingPyQt4">Installing PyQt4</a></h2>
<p>Before installing PyQt4 you need to have Python 2.5 or newer and Qt version 4 installed. You can download Python 2.5 from the main Python website.<a href="#1">[1]</a> Qt version 4 is available from the TrollTech website&#8217;s Downloads section.<a href="#2">[2]</a> Installing PyQt4 on Linux, Windows, or OS X is relatively easy, and I know since I installed it on each of them. You can download the different PyQt4 packages from the Riverbank website, including an all in one (Qt version 4 included) installer for Windows.<a href="#3">[3]</a> There are also installation instructions in the PyQt4 online documentation that are worth a read if you are new to installing software from source.<a href="#4">[4]</a> If you are using a modern Linux distribution, you may want to consider using your package manager to install PyQt4 as it should being along all the necessary dependencies for you.</p>
<h2><a name="FirstApplication">Your First PyQt4 Application</a></h2>
<p>Now that you have PyQT4 properly installed, let’s dive right in and create an application. The first step is to start a new Python file, I called mine <code>PyQt4Intro.py</code> but you can call it whatever you like. Set it up using the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
</span><span class="hl-reserved">import </span><span class="hl-identifier">PyQt4

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	</span><span class="hl-reserved">pass</span></pre></div></div>
<p>If you&#8217;ve read any of my other columns, you may recognize this code as a general template that I use for my Python projects. The major difference in this example is that we import PyQt4. After you have set your file up run the code. If all goes well nothing will happen. If you do get an error, something like the following, it probably means that PyQt4 is not installed properly:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>ImportError: No module named PyQt4</pre></div></div>
<p>Remember that PyQT4 requires Python 2.5. If you have Python 2.5 and older versions of Python installed, try specifying <code>python2.5</code> when you run the script:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>python2.5 PyQt4Intro.py</pre></div></div>
<p>Now that we have everything in order, let&#8217;s create and show a window. After all this is a GUI tutorial! If you are creating a GUI application using PyQt4 (of course, you can also create a non-GUI application using PyQt4), you will need to create a <code>QApplication</code> instance. </p>
<p>The PyQt4 documentation provides a great description of the &#8220;QApplication&#8221; class: &#8220;The QApplication class manages the GUI application&#8217;s control flow and main settings. It contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application&#8217;s initialization and finalization, and provides session management. It also handles most system-wide and application-wide settings. For any GUI application that uses Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any time.&#8221;<a href="#5">[5]</a></p>
<p>What we need to do is create the one and only <code>QApplication</code> instance for our application. To do this, we will import two new modules:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">sys
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtGui</span></pre></div></div>
<p>The first line imports the <code>sys</code> module. This is needed to initialize the <code>QApplication</code>. The second line imports the <code>QtGui</code> module. It allows us to reference <code>QApplication</code> and other <code>QtGui</code> components. </p>
<p>In our main block, we will add the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	# Create the QApplication
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
	</span><span class="hl-comment"># Enter the main loop
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<p>This will create the <code>QApplication</code> instance, initialized with the command-line parameters that may be passed to our script. This is a necessary parameter to the <code>QApplication</code> class and is the reason that we imported the <code>sys</code> module.  The next call is to the <code>exec</code> function which &#8220;enters the main event loop and waits until exit() is called or the main widget is destroyed&#8221;.<a href="#6">[6] </a></p>
<p>If you run the entire script at this point, which I don&#8217;t necessarily recommend since it will seem to hang, you will be running a PyQt4 application. You won&#8217;t see anything since there is no visible component. However, the script will continue to run in its main loop instead of returning right after being executed. If you do decide to run this script, use CTRL+Z to exit.</p>
<h2><a name="TheMainWindow">The Main Window</a></h2>
<p>GUI applications without a visible component are not that useful so let&#8217;s add a window to our application. We will do this by sub-classing the <code>QMainWindow</code> class. The <code>QMainWindow</code> class &#8220;provides a framework for building an application&#8217;s user interface. Qt has &#8220;QMainWindow&#8221; and its related classes for main window management. &#8220;QMainWindow&#8221; has its own layout to which you can add &#8220;QToolBars&#8221;, &#8220;QDockWindows&#8221;, a &#8220;QMenuBar&#8221;, and a &#8220;QStatusBar&#8221;.&#8221;<a href="#7">[7]</a></p>
<p><strong>Note:</strong> There are other ways to create a visible window. For example you could use a <code>QWidget</code> to accomplish much of what we are going to do. I chose to use the <code>QMainWindow</code> since it provides an application framework that you might want to use when building a full GUI application.</p>
<p>We can subclass the <code>QMainWindow</code> as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-code"> = </span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Init the base class
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-brackets">)</span></pre></div></div>
<p>Then in the main block we can create and show our main window like this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
</span><span class="hl-comment">#The Main window
</span><span class="hl-identifier">main_window</span><span class="hl-default"> = </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">()
</span><span class="hl-identifier">main_window</span><span class="hl-default">.</span><span class="hl-identifier">show</span><span class="hl-brackets">()
</span><span class="hl-comment"># Enter the main loop
</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<p>The entire source code thus far is found in Listing 1. When you run the code, you will finally be greeted with your very first PyQt4 window. It will look something similar to Figure 1.</p>
<p><strong>Listing 1</strong></p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
</span><span class="hl-reserved">import </span><span class="hl-identifier">PyQt4
</span><span class="hl-reserved">import </span><span class="hl-identifier">sys
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtGui

</span><span class="hl-reserved">class </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-code"> = </span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Init the base class
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-brackets">)

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	# Create the QApplication
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
	</span><span class="hl-comment">#The Main window
	</span><span class="hl-identifier">main_window</span><span class="hl-default"> = </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">()
	</span><span class="hl-identifier">main_window</span><span class="hl-default">.</span><span class="hl-identifier">show</span><span class="hl-brackets">()
	</span><span class="hl-comment"># Enter the main loop
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<div id="attachment_91" class="wp-caption alignnone" style="width: 216px"><a href="http://www.learningpython.com/wp-content/uploads/2008/09/figure1.png"><img src="http://www.learningpython.com/wp-content/uploads/2008/09/figure1.png" alt="Figure 1" title="Figure 1" width="206" height="128" class="size-medium wp-image-91" /></a><p class="wp-caption-text">Figure 1</p></div>
<h2><a name="AddingSomeWidgets">Adding Some Widgets</a></h2>
<p>A blank window is only slightly more useful then an invisible oen. Therefore our next step is to add some functionality to our window by adding some interactive widgets. In order to do this, we will set the central widget of our main window and use a &#8220;geometry manager&#8221; (or &#8220;layout manager&#8221;) class to manage the positioning of our widgets.</p>
<p>We will add a new function to our <code>HelloWindow</code> class called <code>create_widgets</code> where we will create all of our widgets. We will call this function from the <code>__init__</code> function. The first step in the <code>create_widgets</code> function is to create a widget that will serve as the &#8220;central widget&#8221; for our main window. As mentioned earlier, the <code>QMainWindow</code> class already has a built-in layout making it easy to add menus, toolbars, and other items common to applications. <code>QMainWindow</code> classes also have a &#8220;central widget&#8221; which you can think of as the main part of the application. More to the point, the &#8220;central widget&#8221; is not the toolbars, menu, or docking panes, but the actual functional area of the application. For a greater description of the &#8220;central widget&#8221; see the PyQt4 website.<a href="#8">[8]</a></p>
<p>We will create and set our &#8220;central widget&#8221; using the following code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-identifier">central_widget</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QWidget</span><span class="hl-brackets">()
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">setCentralWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">central_widget</span><span class="hl-brackets">)</span></pre></div></div>
<p>So far this won&#8217;t do anything, since we still have not added anything interactive. To add interactivity, we must add widgets to our &#8220;central widget&#8221;. Rather than place the widgets in static positions, we will use a layout manager to automatically position and adjust our widgets. If you are at all familiar with modern GUI toolkits, the idea of a layout manager should be familiar to you. They serve as a way to &#8220;pack&#8221; widgets into an area where their positions will be relative to each other and to the window. When you resize the window (or area), geometry manager&#8217;s will automatically adjust the size and positions of their child widgets to accommodate the new size.</p>
<p>We will be creating three widgets in our simple application: </p>
<p>1. A <code>QLabel</code> widget to display some static text.<br />
2. A <code>QLineEdit</code> widget to let the user enter some text into an edit field.<br />
3. A <code>QPushButton</code> button widget that will pop up a dialog to display a message when the user clicks on it.</p>
<p>We will create the widgets using the following code. Notice that in the creation of the <code>QLabel</code> and <code>QPushButton</code> widgets we are also setting the text that will appear on each widget. (We can do the same for the <code>QLineEdit</code>, but for now we&#8217;ll leave it blank):</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-comment">#Widgets
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">label</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLabel</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Say hello:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_edit</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLineEdit</span><span class="hl-brackets">()
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_button</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QPushButton</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Push Me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span></pre></div></div>
<p>Next, we will create a <code>QHBoxLayout</code> instance to serve as the central widget&#8217;s layout manager. The <code>QHBoxLayout</code> class is a specific type of layout manager that lays widgets out, and manage their positions, in the horizontal direction. Once we have created the <code>QHBoxLayout</code> instance, we will add our three widgets to it. Adding widgets to a <code>QHBoxLayout</code> will pack them from left to right:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Horizontal layout
</span><span class="hl-identifier">h_box</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QHBoxLayout</span><span class="hl-brackets">()
</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">label</span><span class="hl-brackets">)
</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-brackets">)
</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-brackets">)</span></pre></div></div>
<p>The last step in our widget creation is to set <code>h_box</code> as the layout manager for our central widget:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Create central widget, add layout, and set
</span><span class="hl-identifier">central_widget</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QWidget</span><span class="hl-brackets">()
</span><span class="hl-identifier">central_widget</span><span class="hl-default">.</span><span class="hl-identifier">setLayout</span><span class="hl-brackets">(</span><span class="hl-identifier">h_box</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">setCentralWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">central_widget</span><span class="hl-brackets">)</span></pre></div></div>
<p>The entire code can be seen in Listing 2. When you run this code, you will finally be greeted with a window containing actual widgets. This is illustrated in Figure 2.</p>
<p><strong>Listing 2</strong></p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
</span><span class="hl-reserved">import </span><span class="hl-identifier">PyQt4
</span><span class="hl-reserved">import </span><span class="hl-identifier">sys
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtGui

</span><span class="hl-reserved">class </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-code"> = </span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Init the base class
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">()

	</span><span class="hl-reserved">def </span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Widgets
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">label</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLabel</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Say hello:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_edit</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLineEdit</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_button</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QPushButton</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Push Me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Horizontal layout
		</span><span class="hl-identifier">h_box</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QHBoxLayout</span><span class="hl-brackets">()
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">label</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Create central widget, add layout and set
		</span><span class="hl-identifier">central_widget</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QWidget</span><span class="hl-brackets">()
		</span><span class="hl-identifier">central_widget</span><span class="hl-default">.</span><span class="hl-identifier">setLayout</span><span class="hl-brackets">(</span><span class="hl-identifier">h_box</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">setCentralWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">central_widget</span><span class="hl-brackets">)

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	# Create the QApplication
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
	</span><span class="hl-comment">#The Main window
	</span><span class="hl-identifier">main_window</span><span class="hl-default"> = </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">()
	</span><span class="hl-identifier">main_window</span><span class="hl-default">.</span><span class="hl-identifier">show</span><span class="hl-brackets">()
	</span><span class="hl-comment"># Enter the main loop
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<div id="attachment_92" class="wp-caption alignnone" style="width: 296px"><a href="http://www.learningpython.com/wp-content/uploads/2008/09/figure2.png"><img src="http://www.learningpython.com/wp-content/uploads/2008/09/figure2.png" alt="Figure 2" title="Figure 2" width="286" height="128" class="size-medium wp-image-92" /></a><p class="wp-caption-text">Figure 2</p></div>
<h2><a name="SignalHandling">Signal Handling</a></h2>
<p>Responding to the user’s interaction with the GUI is something that must be done in all GUI applications. Therefore the next step in constructing a GUI application is signal or event handling. If you are unfamiliar with these terms you can thing of it his way: when the end user interacts with your GUI, her interaction will causes signals to be sent to your application. So if she clicks on a button the clicked() signal will be sent. If your application wants to do something when that button is clicked, then you will have to handle that widgets <code>clicked()</code> signal.  In our example, we are going to show a pop up a dialog when the user clicks on our button widget.</p>
<p>I find connecting to signals in PyQt4 a bit odd because of a built in macro that needs to be called but the process is relatively easy so I shouldn&#8217;t complain too much. The idea is similar to that of other Python GUI toolkits: you connect functions that you have written to signals or events that are emitted by a widget. So whenever a widget emits a signal that has been connected to a function, that function will be called.</p>
<p><strong>Note:</strong> Qt goes a bit further with the idea of built-in &#8220;slots&#8221; but for the sake of this tutorial we will be ignoring them. For more information on &#8220;slots&#8221; please see the PyQt4 documentation.</p>
<p>In order for the &#8220;QPushButton&#8221; to do something when pushed, we have to connect a function in our <code>HelloWindow</code> class with the <code>clicked</code> signal.  To do this, we need to call the <code>QObject</code> classes <code>connect</code> static function. We must tell it three things: </p>
<p>1. The <code>QWidget</code> that will emit the signal.<br />
2. The signal that we want to connect to.<br />
3. The function that should be called when the <code>QWidget</code> emits the signal.</p>
<p>In our example, we will do this like so:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#connect signal
</span><span class="hl-identifier">QtCore</span><span class="hl-default">.</span><span class="hl-identifier">QObject</span><span class="hl-default">.</span><span class="hl-identifier">connect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-code">
	, </span><span class="hl-identifier">QtCore</span><span class="hl-code">.</span><span class="hl-identifier">SIGNAL</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">clicked()</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
	, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">)</span></pre></div></div>
<p><strong>Note:</strong> We could just as easily have called <code>self.connect</code> since a <code>QMainWindow</code> is a descendant of the <code>QObject</code> class.</p>
<p><code>QtCore.SIGNAL</code> is a macro that you have to use for the second parameter. It accepts the signal name as a string (in this case <code>clicked()</code>) and converts it into an object that is required for the signal connection. The documentation is a bit sparse regarding exactly what the macro does, but luckily for us we don&#8217;t really need to know the details.</p>
<p>In order for this code to compile, we also have to import the <code>QtCore</code> module:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtCore</span></pre></div></div>
<p>We also have to add a new function to the <code>HelloWindow</code> class. This is the function <code>on_hello_clicked</code> that will respond to the <code>clicked()</code> signal:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Clicked</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>When you run the code in Listing 3, you will see &#8220;Clicked&#8221; printed out to the command line every time you click the &#8220;Push Me!&#8221; button.</p>
<p><strong>Listing 3</strong></p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
</span><span class="hl-reserved">import </span><span class="hl-identifier">PyQt4
</span><span class="hl-reserved">import </span><span class="hl-identifier">sys
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtGui
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtCore

</span><span class="hl-reserved">class </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-code"> = </span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Init the base class
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">()


	</span><span class="hl-reserved">def </span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Widgets
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">label</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLabel</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Say hello:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_edit</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLineEdit</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_button</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QPushButton</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Push Me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)

		</span><span class="hl-comment">#connect signal
		</span><span class="hl-identifier">QtCore</span><span class="hl-default">.</span><span class="hl-identifier">QObject</span><span class="hl-default">.</span><span class="hl-identifier">connect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-code">
			, </span><span class="hl-identifier">QtCore</span><span class="hl-code">.</span><span class="hl-identifier">SIGNAL</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">clicked()</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
			, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">)


		</span><span class="hl-comment">#Horizontal layout
		</span><span class="hl-identifier">h_box</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QHBoxLayout</span><span class="hl-brackets">()
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">label</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Create central widget, add layout and set
		</span><span class="hl-identifier">central_widget</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QWidget</span><span class="hl-brackets">()
		</span><span class="hl-identifier">central_widget</span><span class="hl-default">.</span><span class="hl-identifier">setLayout</span><span class="hl-brackets">(</span><span class="hl-identifier">h_box</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">setCentralWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">central_widget</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Clicked</span><span class="hl-quotes">&quot;
		
</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	# Create the QApplication
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
	</span><span class="hl-comment">#The Main window
	</span><span class="hl-identifier">main_window</span><span class="hl-default"> = </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">()
	</span><span class="hl-identifier">main_window</span><span class="hl-default">.</span><span class="hl-identifier">show</span><span class="hl-brackets">()
	</span><span class="hl-comment"># Enter the main loop
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<h2><a name="DisplayingAMessage">Displaying a Message</a></h2>
<p>Now that we have a function connected to the clicking of the button widget we need to read the text from the <code>QLineEdit</code> widget and display it to the user. Fortunately both tasks are quite easy in PyQt4. </p>
<p>In order to get the text from a <code>QLineEdit</code> widget, the <code>QLineEdit</code> classes member function <code>displayText</code> needs to be called. The <code>displayText</code> function simply returns the contents of the <code>QLineEdit</code> widget.</p>
<p>Showing a pop-up dialog using PyQt4 is just as easy as getting the contents of a <code>QLineEdit</code> widget, thanks to the <code>QMesssageBox</code> classes static function <code>information</code>.  This function allows you to set the title, text and what buttons are on a simple &#8220;information dialog&#8221;.  Calling it from our PyQt4 application in response to the button click is very simple:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">information</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
		, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello!</span><span class="hl-quotes">&quot;</span><span class="hl-code">
		, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello %s</span><span class="hl-quotes">&quot;</span><span class="hl-code"> % </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-code">.</span><span class="hl-identifier">displayText</span><span class="hl-brackets">()</span><span class="hl-code">
		, </span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMessageBox</span><span class="hl-code">.</span><span class="hl-identifier">Ok</span><span class="hl-brackets">)</span></pre></div></div>
<p>In this code, we call the information function, set its title text to be &#8220;Hello!&#8221;, set its main message to be &#8220;Hello &#8221; followed by the contents of the <code>hello_edit</code> <code>QInputEdit</code>, and give it one button, an &#8220;ok&#8221; button. All of the code is found in Listing 4. What this looks like running on Linux, Windows and OS X can be seen in Figure 3, Figure 4, and Figure 5 respectively.</p>
<p><strong>Listing 4</strong></p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
</span><span class="hl-reserved">import </span><span class="hl-identifier">PyQt4
</span><span class="hl-reserved">import </span><span class="hl-identifier">sys
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtGui
</span><span class="hl-reserved">from </span><span class="hl-identifier">PyQt4 </span><span class="hl-reserved">import </span><span class="hl-identifier">QtCore

</span><span class="hl-reserved">class </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-code"> = </span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Init the base class
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMainWindow</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">win_parent</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">()


	</span><span class="hl-reserved">def </span><span class="hl-identifier">create_widgets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-comment">#Widgets
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">label</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLabel</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Say hello:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_edit</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QLineEdit</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">hello_button</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QPushButton</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Push Me!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)

		</span><span class="hl-comment">#connect signal
		</span><span class="hl-identifier">QtCore</span><span class="hl-default">.</span><span class="hl-identifier">QObject</span><span class="hl-default">.</span><span class="hl-identifier">connect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-code">
			, </span><span class="hl-identifier">QtCore</span><span class="hl-code">.</span><span class="hl-identifier">SIGNAL</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">clicked()</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
			, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">)


		</span><span class="hl-comment">#Horizontal layout
		</span><span class="hl-identifier">h_box</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QHBoxLayout</span><span class="hl-brackets">()
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">label</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-brackets">)
		</span><span class="hl-identifier">h_box</span><span class="hl-default">.</span><span class="hl-identifier">addWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_button</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Create central widget, add layout and set
		</span><span class="hl-identifier">central_widget</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QWidget</span><span class="hl-brackets">()
		</span><span class="hl-identifier">central_widget</span><span class="hl-default">.</span><span class="hl-identifier">setLayout</span><span class="hl-brackets">(</span><span class="hl-identifier">h_box</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">setCentralWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">central_widget</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">on_hello_clicked</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">information</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
			, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello!</span><span class="hl-quotes">&quot;</span><span class="hl-code">
			, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello %s</span><span class="hl-quotes">&quot;</span><span class="hl-code"> % </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">hello_edit</span><span class="hl-code">.</span><span class="hl-identifier">displayText</span><span class="hl-brackets">()</span><span class="hl-code">
			, </span><span class="hl-identifier">QtGui</span><span class="hl-code">.</span><span class="hl-identifier">QMessageBox</span><span class="hl-code">.</span><span class="hl-identifier">Ok</span><span class="hl-brackets">)

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
	</span><span class="hl-comment"># Someone is launching this directly
	# Create the QApplication
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">QtGui</span><span class="hl-default">.</span><span class="hl-identifier">QApplication</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">)
	</span><span class="hl-comment">#The Main window
	</span><span class="hl-identifier">main_window</span><span class="hl-default"> = </span><span class="hl-identifier">HelloWindow</span><span class="hl-brackets">()
	</span><span class="hl-identifier">main_window</span><span class="hl-default">.</span><span class="hl-identifier">show</span><span class="hl-brackets">()
	</span><span class="hl-comment"># Enter the main loop
	</span><span class="hl-identifier">app</span><span class="hl-default">.</span><span class="hl-identifier">exec_</span><span class="hl-brackets">()</span></pre></div></div>
<div id="attachment_93" class="wp-caption alignnone" style="width: 310px"><a href="http://www.learningpython.com/wp-content/uploads/2008/09/figure3.png"><img src="http://www.learningpython.com/wp-content/uploads/2008/09/figure3-300x217.png" alt="Figure 3" title="Figure 3" width="300" height="217" class="size-medium wp-image-93" /></a><p class="wp-caption-text">Figure 3</p></div>
<div id="attachment_94" class="wp-caption alignnone" style="width: 310px"><a href="http://www.learningpython.com/wp-content/uploads/2008/09/figure4.png"><img src="http://www.learningpython.com/wp-content/uploads/2008/09/figure4-300x220.png" alt="Figure 4" title="Figure 4" width="300" height="220" class="size-medium wp-image-94" /></a><p class="wp-caption-text">Figure 4</p></div>
<div id="attachment_95" class="wp-caption alignnone" style="width: 310px"><a href="http://www.learningpython.com/wp-content/uploads/2008/09/figure5.png"><img src="http://www.learningpython.com/wp-content/uploads/2008/09/figure5-300x230.png" alt="Figure 5" title="Figure 5" width="300" height="230" class="size-medium wp-image-95" /></a><p class="wp-caption-text">Figure 5</p></div>
<h2><a name="Conclusion">Conclusion</a></h2>
<p>That&#8217;s it for this quick introduction to PyQt4. All-in-all, I was pleasantly surprised by PyQt4. Even during my first foray into the toolkit, I found it very straightforward and was able to get things going in a matter of minutes. Granted, what I was trying to accomplish was pretty simple, but it&#8217;s always a good sign when doing something simple is easy!</p>
<p>You should now have enough information to go and create your very own &#8220;GUIfied&#8221; applications. Of course, there is much, much more to PyQt4 than I have covered in this tutorial. But if you use the PyQt4 documentation and the fundamentals from this tutorial, you shouldn&#8217;t have much trouble moving forward.</p>
<p><a name="1">[1]</a> <a href="http://www.python.org/download/">http://www.python.org/download/</a><br />
<a name="2">[2]</a> <a href="http://www.riverbankcomputing.co.uk/pyqt/download.php">http://www.riverbankcomputing.co.uk/pyqt/download.php</a><br />
<a name="3">[3]</a> <a href="http://trolltech.com/downloads">http://trolltech.com/downloads</a><br />
<a name="4">[4]</a> <a href="http://www.riverbankcomputing.com/Docs/PyQt4/pyqt4ref.html#installing-pyqt">http://www.riverbankcomputing.com/Docs/PyQt4/pyqt4ref.html#installing-pyqt</a><br />
<a name="5">[5]</a> <a href="http://www.riverbankcomputing.com/Docs/PyQt4/html/qapplication.html">http://www.riverbankcomputing.com/Docs/PyQt4/html/qapplication.html</a><br />
<a name="6">[6]</a> <a href="http://www.riverbankcomputing.com/Docs/PyQt4/html/qapplication.html#exec">http://www.riverbankcomputing.com/Docs/PyQt4/html/qapplication.html#exec</a><br />
<a name="7">[7]</a> <a href="http://www.riverbankcomputing.com/Docs/PyQt4/html/qmainwindow.html#details">http://www.riverbankcomputing.com/Docs/PyQt4/html/qmainwindow.html#details</a><br />
<a name="8">[8]</a> <a href="http://www.riverbankcomputing.com/Docs/PyQt4/html/qmainwindow.html#details">http://www.riverbankcomputing.com/Docs/PyQt4/html/qmainwindow.html#details</a></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2008/09/20/an-introduction-to-pyqt/&title=An Introduction to PyQt: creating GUIs with Python's QT bindings&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2008/09/20/an-introduction-to-pyqt/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
