This section provides an overview of what PyQt4
is, and why a developer might want to use it.
PyQt is a GUI widgets toolkit. It is a Python interface for Qt, one of the most powerful, and popular cross-platform GUI library. PyQt is a blend of Python programming language and the Qt library. This introductory tutorial will assist you in creating graphical applications with the help of PyQt.
It should also mention any large subjects within PyQt4
, and link out to the related topics. Since the Documentation for PyQt4
is new, you may need to create initial versions of those related topics.
Creating a simple GUI application using PyQt involves the following steps −
Import QtGui module
Create an application object.
A QWidget object creates top level window. Add QLabel object in it.
Set the caption of label as “hello world”.
Define the size and position of window by setGeometry() method.
Enter the mainloop of application by app.exec_() method.
In the following example, two QPushButton
objects (b1
and b2
) are added in QDialog
window. We want to call functions b1_clicked()
and b2_clicked()
on clicking b1
and b2
respectively.
When b1
is clicked, the clicked()
signal is connected to b1_clicked()
function
b1.clicked.connect(b1_clicked())
When b2
is clicked, the clicked()
signal is connected to b2_clicked()
function
QObject.connect(b2, SIGNAL("clicked()"), b2_clicked)
Widgets used to build the GUI interface act as the source of such events.
Each PyQt widget, which is derived from QObject
class, is designed to emit signal in response to one or more events. The signal on its own does not perform any action. Instead, it is connected to a slot. The slot can be any callable Python function.
A QLabel
object acts as a placeholder to display non-editable text,image, or a animated GIF. It can also be used as a mnemonic key for other widgets.
In this example, QLabel
objects l2
and l4
have the caption containing hyperlink. setOpenExternalLinks
for l2
is set to true. Hence, if this label is clicked, the associated URL will open in the browser. linkHovered
signal of l4
is connected to hovered()
function. So, whenever the mouse hovers over it, the function will be executed.
QPixmap object prepares offscreen image from python.jpg file. It is displayed as label l3
by using setPixmap()
method
Plain text, hyperlink or rich text can also be displayed in QLabel
.
In QLabel
most of the basic HTML tags are allowed. For Example : h1
,h2
,h3
,font
,span
,etc