Saturday, September 3, 2011

PyQt: power of Python, beauty of Qt

Qt is very powerful tool for GUI programming. But programming using Qt C++ is not always easy because of unavailability of libraries and difficulty in coding. But Python is powerful and has abundant list of libraries related to virtually anything. Some operations in Python is very easy to use. But the problem of Python is that, it has no GUI its own as handy. PyGTK (Python and GTK) and PyQt (Python and Qt) are the remedy. Here I am explaining how to start with PyQt.
PyQt has no support from QtCreator yet, but the features of QtDesigner can also be used for the PyQt coding. This tutorial explains how to start PyQt programming if you are already aware of Qt C++ programming. This is not just a 'hello world' program, but the code will do something and make you capable of coding your own programs.
In this example, we are building a simple GUI with three LineEdit bars and a single button. When the button is clicked, the numbers entered in the LineEdit a and b are added and the sum is shown in the third LineEdit.


First of all, make GUI widget in Qt Designer and rename the objects a, b, sum and pushButton as shown in screen-shot below. Also make a custom slot named calc_sum(). Save and quit the Designer program.


Now you can see the user interface file named 'widget.ui' (the filename may vary if you changed the widget name while creating your project. Anyway, the filename with .ui as extension is the file we need). We need to create the curresponding python file for user interface. In C++, it is done using uic(User Interface Compiler). In our case, in python, we use a utility named 'pyuic4'. If 'pyuic4' will be automatically installed while you install PyQt. If not, especially in Linux, install it from the repository. Then make the user interface file 'calc_ui.py' (or any name you wish. But for the first time use this name to avoid ambiguity) file using the command:

pyuic4 widget.ui > calc_ui.py

This command will generate a new file named 'cal_ui.py'. This is the user interface file only. We are going to make another file 'calc.py' which will handle the core program. Use a text editor and copy the code below:

import sys
from PyQt4 import QtCore, QtGui

from calc_ui import Ui_Widget


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Widget()
        self.ui.setupUi(self)
    def calc_sum(Widget):
    value1=Widget.ui.a.text();
    value2=Widget.ui.b.text();
    result=float(value1)+float(value2);
    str_result=str(result)
    Widget.ui.sum.setText(str_result);


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

Save the file. Then run the file:

pyhon calc.py
I am not so good in explaining the code. I think you got the idea of how to make a custom slot and to make a standalone program. If any reader need clarification or more explanation, I will try to explain further.