100% found this document useful (1 vote)
578 views

Getting Started With PyQt4 v1

PyQt is a set of python bindings to the qt application framework. It provides access to features which include: Widgets and other graphical user interface controls Database management and querying XML handling and processing Graphics and multimedia Web browser integration and networking. Pyqt is available under the GNU General Public License.

Uploaded by

Medhat Dawoud
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
578 views

Getting Started With PyQt4 v1

PyQt is a set of python bindings to the qt application framework. It provides access to features which include: Widgets and other graphical user interface controls Database management and querying XML handling and processing Graphics and multimedia Web browser integration and networking. Pyqt is available under the GNU General Public License.

Uploaded by

Medhat Dawoud
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Outline

Overview
Installing PyQt
Hello World in PyQt
Widgets

Getting Started with PyQt4

David Boddie
[email protected]

7th February 2009

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt
Hello World in PyQt
Widgets

Overview

Installing PyQt
Checking the Installation

Hello World in PyQt


PyQt Concepts – The Event Loop

Widgets
Widgets and Layouts
PyQt Concepts – Parents and Children

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt
Hello World in PyQt
Widgets

Overview

PyQt is a set of Python bindings to the Qt application framework,


providing access to features which include:
I Widgets and other graphical user interface controls
I Database management and querying
I XML handling and processing
I Graphics and multimedia
I Web browser integration and networking
PyQt is available under the GNU General Public License.

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt Checking the Installation
Hello World in PyQt
Widgets

Installing Qt and PyQt


Both Qt and PyQt are available as standard in many GNU/Linux
distributions:
I Debian, Ubuntu: python-qt4 and pyqt4-dev-tools
I openSUSE: python-qt4
I Mandriva: python-qt4
I Fedora: PyQt4
I Pardus: PyQt4
The PyQt download page contains:
I Binary installers for Windows
I Source packages for Mac OS X, Windows, Unix and
GNU/Linux

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt Checking the Installation
Hello World in PyQt
Widgets

Checking the Installation

Start a Python session in a terminal and type:


from PyQt4.QtCore import QT VERSION STR
print QT VERSION STR
The version of Qt in use should be printed to the terminal.
>>> from PyQt4.QtCore import QT VERSION STR
- 32pt- 32pt>>> print QT VERSION STR
4.3.2

The version reported will depend on your PyQt installation.

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt PyQt Concepts – The Event Loop
Hello World in PyQt
Widgets

Hello World in PyQt

Here’s a simple PyQt application:


import sys
from PyQt4.QtGui import QApplication, QPushButton
app = QApplication(sys.argv)
button = QPushButton("Hello world!")
button.show()
sys.exit(app.exec ())
1. Creates an application object
2. Creates a button
This does four things:
3. Shows the button
4. Runs the event loop

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Installing PyQt PyQt Concepts – The Event Loop
Hello World in PyQt
Widgets

PyQt Concepts – The Event Loop

The application communicates with the system via events.


I We do not dispatch events ourselves.
I We ask the QApplication object to run an event loop.
I The event loop controls the execution of the application.
I We give up control when it starts running, but it calls
functions and methods that we provide.
The application won’t work unless we start an event loop by calling
the QApplication object’s exec () method.

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Widgets and Layouts
Installing PyQt
PyQt Concepts – Parents and Children
Hello World in PyQt
Widgets

Widgets

Widgets are graphical elements that the user interacts with.


checkbox = QCheckBox("C&ase sensitive")
combobox = QComboBox()
combobox.addItem("Large (L)")
spinbox = QSpinBox()

QCheckBox QComboBox QSpinBox

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Widgets and Layouts
Installing PyQt
PyQt Concepts – Parents and Children
Hello World in PyQt
Widgets

Widgets and Layouts

Widgets can be used as containers to hold other widgets.


window = QWidget()
label = QLabel("Name:")
editor = QLineEdit()
layout = QHBoxLayout(window)
layout.addWidget(label)
layout.addWidget(editor)
window.show()

I The label, line edit and window are created in the same way.
I The layout puts the label and line edit inside the widget.
I The window is the parent of both the label and line edit.

David Boddie [email protected] Getting Started with PyQt4


Outline
Overview
Widgets and Layouts
Installing PyQt
PyQt Concepts – Parents and Children
Hello World in PyQt
Widgets

PyQt Concepts – Parents and Children


Container widgets are parents of the widgets inside them:
I Windows have no parents.
I Child widgets can have their own children.
I Children of a widget are only visible if their parent is.
Parent widgets “own” their children:
def create window():
window = QWidget()
editor = QLineEdit()
layout = QHBoxLayout(window)
layout.addWidget(editor)
return window
The editor and layout objects go out of scope but are not deleted.
David Boddie [email protected] Getting Started with PyQt4

You might also like