Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python PyQt Common Widgets

Python PyQt Common Widgets

PyQt is a GUI programming toolkit for Python; it is the Python binding for the Qt application framework.

Qt is a cross-platform C++ graphical user interface application development framework, widely used for building GUI programs.

PyQt provides a rich set of widgets for building user interfaces; these widgets are the basic building blocks of a GUI application.


Basic window widgets

QMainWindow

QMainWindow is the main window class and provides a standard application framework:

Example

from

PyQt5.

QtWidgets

import

QMainWindow

,

QApplication

app

=

QApplication

(

[

]

)

window

=

QMainWindow

(

)

window.

setWindowTitle

(

'Main Window Example'

)

window.

setGeometry

(

100

,

100

,

800

,

600

)

 

x, y, width, height

window.

show

(

)

app.

exec_

(

)

QWidget

QWidget is the base class of all user interface objects:

Example

from

PyQt5.

QtWidgets

import

QWidget

widget

=

QWidget

(

)

widget.

setWindowTitle

(

'Basic Window'

)

widget.

show

(

)


Common input widgets

QLabel

Display text or an image:

Example

from

PyQt5.

QtWidgets

import

QLabel

label

=

QLabel

(

'This is a label'

)

label.

setStyleSheet

(

'font-size: 16px; color: blue;'

)

QLineEdit

Single-line text input:

Example

from

PyQt5.

QtWidgets

import

QLineEdit

line_edit

=

QLineEdit

(

)

line_edit.

setPlaceholderText

(

'Please enter something...'

)

QTextEdit

Multi-line text editor:

Example

from

PyQt5.

QtWidgets

import

QTextEdit

text_edit

=

QTextEdit

(

)

text_edit.

setPlainText

(

'Multi-line text editor'

)

QComboBox

Drop-down selection box:

Example

from

PyQt5.

QtWidgets

import

QComboBox

combo

=

QComboBox

(

)

combo.

addItems

(

[

'Option 1'

,

'Option 2'

,

'Option 3'

]

)


Button widgets

QPushButton

Normal button:

Example

from

PyQt5.

QtWidgets

import

QPushButton

button

=

QPushButton

(

'Click me'

)

button.

setStyleSheet

(

'background-color: green; color: white;'

)

QRadioButton

Radio button:

Example

from

PyQt5.

QtWidgets

import

QRadioButton

radio1

=

QRadioButton

(

'Option 1'

)

radio2

=

QRadioButton

(

'Option 2'

)

QCheckBox

Checkbox:

Example

from

PyQt5.

QtWidgets

import

QCheckBox

check1

=

QCheckBox

(

'Choice 1'

)

check2

=

QCheckBox

(

'Choice 2'

)


Layout widgets

QHBoxLayout

Horizontal layout:

Example

from

PyQt5.

QtWidgets

import

QHBoxLayout

,

QPushButton

h_layout

=

QHBoxLayout

(

)

h_layout.

addWidget

(

QPushButton

(

'Left'

)

)

h_layout.

addWidget

(

QPushButton

(

'Center'

)

)

h_layout.

addWidget

(

QPushButton

(

'Right'

)

)

QVBoxLayout

Vertical layout:

Example

from

PyQt5.

QtWidgets

import

QVBoxLayout

,

QPushButton

v_layout

=

QVBoxLayout

(

)

v_layout.

addWidget

(

QPushButton

(

'Top'

)

)

v_layout.

addWidget

(

QPushButton

(

'Center'

)

)

v_layout.

addWidget

(

QPushButton

(

'Bottom'

)

)

QGridLayout

Grid layout:

Example

from

PyQt5.

QtWidgets

import

QGridLayout

,

QPushButton

grid

=

QGridLayout

(

)

grid.

addWidget

(

QPushButton

(

'(0,0)'

)

,

0

,

0

)

grid.

addWidget

(

QPushButton

(

'(0,1)'

)

,

0

,

1

)

grid.

addWidget

(

QPushButton

(

'(1,0)'

)

,

1

,

0

)


Advanced widgets

QTableWidget

Table widget:

Example

from

PyQt5.

QtWidgets

import

QTableWidget

,

QTableWidgetItem

table

=

QTableWidget

(

3

,

3

)

 

3 rows and 3 columns

table.

setHorizontalHeaderLabels

(

[

'Column 1'

,

'Column 2'

,

'Column 3'

]

)

table.

setItem

(

0

,

0

,

QTableWidgetItem

(

'Data 1'

)

)

QTreeWidget

Tree widget:

Example

from

PyQt5.

QtWidgets

import

QTreeWidget

,

QTreeWidgetItem

tree

=

QTreeWidget

(

)

tree.

setHeaderLabels

(

[

'Name'

,

'Value'

]

)

root

=

QTreeWidgetItem

(

tree

,

[

'Root node'

]

)

child

=

QTreeWidgetItem

(

root

,

[

'Child node'

,

'123'

]

)

QTabWidget

Tab widget:

Example

from

PyQt5.

QtWidgets

import

QTabWidget

,

QWidget

tab

=

QTabWidget

(

)

tab.

addTab

(

QWidget

(

)

,

'Tab 1'

)

tab.

addTab

(

QWidget

(

)

,

'Tab 2'

)


Dialog widgets

QMessageBox

Message dialog:

Example

from

PyQt5.

QtWidgets

import

QMessageBox

msg

=

QMessageBox

(

)

msg.

setWindowTitle

(

'Notice'

)

msg.

setText

(

'This is a message dialog'

)

msg.

setIcon

(

QMessageBox.

Information

)

msg.

exec_

(

)

QFileDialog

File dialog:

Example

from

PyQt5.

QtWidgets

import

QFileDialog

file_name

=

QFileDialog.

getOpenFileName

(

None

,

'Select a file'

,

''

,

'Text files (.txt);;All files ()'

)


Styling

PyQt supports CSS styles to beautify widgets:

Example

button

=

QPushButton

(

'Styled button'

)

button.

setStyleSheet

(

'''
    QPushButton {
        background-color: #4CAF50;
        border: none;
        color: white;
        padding: 10px 24px;
        text-align: center;
        font-size: 16px;
        margin: 4px 2px;
        border-radius: 8px;
    }
    QPushButton:hover {
        background-color: #45a049;
    }
'''

)