Official website for Linux User & Developer
FOLLOW US ON:
Jul
28

Python development masterclass

by Kunal Deo

It may seem like Python is just another scripting language, but know that when you are using Linux, the chances are there is some Python code working backstage helping you. Among other things, this article looks into the versatile nature of Python and its many uses…

PyQt
Yes, you have guessed right. PyQt is a library (or rather binding) that lets you build GUI applications using Python and Qt. PyQt brings several advantages inherited directly from Qt.

Examples are:
1. Near native application UI across a wide range of platforms. Qt works natively on all major platforms including Linux, Mac OS X
and Windows.
2. Uses the innovative signals/slots paradigm to couple GUI items and actions.
3. PyQt binds to most of the Qt components including its non GUI and web components.
4. Allows subclassing of Qt classes in Python.
5. Comes with an enormous inventory of advanced GUI controls, such as a canvas, an editable table module and a rich text editor.
6. KDE libraries are also accessible via PyQt; this enables Python developers to build native KDE applications using it.
PyQt is developed by the British firm Riverbank Computing. It is available under similar terms to Qt versions older than 4.5; this means a variety of licences including GNU General Public License (GPL) and a commercial licence, but not the GNU Lesser General Public License (LGPL). This restricts commercial software makers from using it for free without making their product GPL. Nokia is trying to get around that by releasing its own Python implementation of Qt called PySide which is licensed under LGPL, the same licence that Qt uses.

PyQt4 makes use of most of the existing Qt components like QtCore (for core non-GUI classes), QtGui (GUI interface classes), QtNetwork (for TCP/HTTP/UDP/FTP networking support), QtOpenGL (for GPU accelerated OpenGL 3D rendering), QtSql (contains modules supporting a wide range of open source and proprietary databases), QtSvg (provides static SVG support), QtXml (Sax and Dom interfaces for XML parser), QtMultimedia (low-level multimedia API), QtDesigner (contains classes that allow Qt Designer to be extended using PyQt.) and UIC (provides support for handling the XML files created by Qt Designer). Like PyGTK, PyQt also helps developers to write cross-platform applications without recompiling them.

Hello PyQt
Time to build a basic PyQt application. The following application that we will be developing is a minimalist text editor.

Code for:hellopyqt.py

@description: PyQt4 Text Editor

import sys

#import PyQt4 modules
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
 def __init__(self):
 QtGui.QMainWindow.__init__(self)

 self.resize(350, 250)
 self.setWindowTitle(‘PyQt4 Text Editor’)

 textEdit = QtGui.QTextEdit()
 self.setCentralWidget(textEdit)

 # Sets up  the default icon (find it on disc), keyboard shortcut and status bar
 exit = QtGui.QAction(QtGui.QIcon(‘icons/exit.png’), ‘Exit’, self)
 exit.setShortcut(‘Ctrl+Q’)
 exit.setStatusTip(‘Exit application’)

 # Connect the triggered() signal to close () slot.
 self.connect(exit, QtCore.SIGNAL(‘triggered()’), QtCore.SLOT(‘close()’))

 self.statusBar()

 # Add menubars and toolbars
 menubar = self.menuBar()
 file = menubar.addMenu(‘&File’)
 file.addAction(exit)

 toolbar = self.addToolBar(‘Exit’)
 toolbar.addAction(exit)

#Every PyQt4 application must create an application object. The application object is located in
#the QtGui module. The sys.argv parameter is a list of arguments from the command line. Python #scripts can be run #from the shell.

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()

#The event handling starts from this point. The mainloop receives events from
#the window system #and dispatches them to the application widgets. The
#mainloop ends, if we call the exit() method #or the main widget is
#destroyed. The sys.exit() method ensures a clean exit.

sys.exit(app.exec_())

#The QWidget widget is the base class of all user interface objects in PyQt4. We are using
#QTextEdit to provide text editing functionality to the #application

textEdit = QtGui.QTextEdit()
self.setCentralWidget(textEdit)
@

Next Page
twitter follow us

Pages: 1 2 3 4 5 6
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    2 Comments »

    • Frank Townsend said:

      Please state which old python version and other programs you are using. It is obvious from your print statements that it is not 3.1.2 which is recommended for ALL new development.

    • HarryD said:

      @Frank Python 3.1.2 is not recommended for development as of now. If you had read the article completely you would have known that Python currently maintains two mutually incompatible branch of the 2.X and 3.X. Until 3.X is finalized 2.X will remain in the usage. FYI: 2.X is still used in 95% of the installed applications.

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields