YouTip LogoYouTip

Python Pyqt Widgets

\n\n\n Python PyQt Common Widgets | Rookie Tutorial\n\n\n\n

Python3.x Python PyQt Common Widgets

\n\n

PyQt is a GUI programming toolkit for the Python language. It is a Python binding for the Qt application framework.

\n\n

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

\n\n

PyQt provides rich components (Widgets) to build user interfaces. These components are the foundational building blocks for GUI applications.

\n\n
\n\n

Basic Window Components

\n\n

QMainWindow

\n\n

QMainWindow is the main window class, providing a standard application framework:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QMainWindow, QApplication\n\napp = QApplication([])\nwindow = QMainWindow()\nwindow.setWindowTitle('Main Window Example')\nwindow.setGeometry(100,100,800,600)# x, y, width, height\nwindow.show()\napp.exec_()
\n\n

QWidget

\n\n

QWidget is the base class for all user interface objects:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QWidget\n\nwidget = QWidget()\nwidget.setWindowTitle('Basic Window')\nwidget.show()
\n\n
\n\n

Common Input Components

\n\n

QLabel

\n\n

Displays text or images:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QLabel\n\nlabel = QLabel('This is a label')\nlabel.setStyleSheet('font-size: 16px; color: blue;')
\n\n

QLineEdit

\n\n

Single-line text input box:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QLineEdit\n\nline_edit = QLineEdit()\nline_edit.setPlaceholderText('Please enter content...')
\n\n

QTextEdit

\n\n

Multi-line text editor:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QTextEdit\n\ntext_edit = QTextEdit()\ntext_edit.setPlainText('Multi-line Text Editor')
\n\n

QComboBox

\n\n

Drop-down selection box:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QComboBox\n\ncombo = QComboBox()\ncombo.addItems(['Option 1','Option 2','Option 3'])
\n\n
\n\n

Button Components

\n\n

QPushButton

\n\n

Standard button:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QPushButton\n\nbutton = QPushButton('Click Me')\nbutton.setStyleSheet('background-color: green; color: white;')
\n\n

QRadioButton

\n\n

Radio button:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QRadioButton\n\nradio1 = QRadioButton('Option 1')\nradio2 = QRadioButton('Option 2')
\n\n

QCheckBox

\n\n

Checkbox:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QCheckBox\n\ncheck1 = QCheckBox('Choice 1')\ncheck2 = QCheckBox('Choice 2')
\n\n
\n\n

Layout Components

\n\n

QHBoxLayout

\n\n

Horizontal layout:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QHBoxLayout, QPushButton\n\nh_layout = QHBoxLayout()\nh_layout.addWidget(QPushButton('Left'))\nh_layout.addWidget(QPushButton('Center'))\nh_layout.addWidget(QPushButton('Right'))
\n\n

QVBoxLayout

\n\n

Vertical layout:

\n\n

Example

\n\n\n
from PyQt5.QtWidgets import QVBoxLayout, QPushButton\n\nv_layout = QVBoxLayout()\nv_layout.addWidget(QPushButton('Top'))\nv_layout.addWidget(QPushButton('Middle'))\nv_layout.addWidget(QPushButton('Bottom'))
\n\n

QGridLayout

\n\n

Grid layout:

\n\n

Example

\n\n
from PyQt5.QtSpeed Racing Official Account s import QGridLayout, QPushButton\n\ngrid = QGridLayout()\ngrid.addWidget(QPushButton('(0,0)'),0,0)\ngrid.addWidget(QPushButton('(0,1)'),0,1)\ngrid.addWidget(QPushButton('(1,0)'),1,0)
\n\n
\n\n

Advanced Components

\n\n

QTableWidget

\n\n

Table component:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem\n\ntable = QTableWidget(3,3)# 3 rows, 3 columns\ntable.setHorizontalHeaderLabels(['Column 1','Column 2','Column 3'])\ntable.setItem(0,0, QTableWidgetItem('Data 1'))
\n\n

QTreeWidget

\n\n

Tree component:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem\n\ntree = QTreeWidget()\ntree.setHeaderLabels(['Name','Value'])\nroot = QTreeWidgetItem(tree,['Root Node'])\nchild = QTreeWidgetItem(root,['Child Node','123'])
\n\n

QTabWidget

\n\n

Tab component:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QTabWidget, QWidget\n\ntab = QTabWidget()\ntab.addTab(QWidget(),'Tab 1')\ntab.addTab(QWidget(),'Tab 2')
\n\n
\n\n

Dialog Components

\n\n

QMessageBox

\n\n

Message dialog:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QMessageBox\n\nmsg = QMessageBox()\nmsg.setWindowTitle('Prompt')\nmsg.setText('This is a message dialog')\nmsg.setIcon(QMessageBox.Information)\nmsg.exec_()
\n\n

QFileDialog

\n\n

File dialog:

\n\n

Example

\n\n
from PyQt5.QtWidgets import QFileDialog\n\nfile_name = QFileDialog.getOpenFileName(None,'Select File','','Text Files (*.txt);;All Files (*)')
\n\n
\n\n

Styling

\n\n

PyQt supports using CSS styles to beautify components:

\n\n

Example

\n\n
button = QPushButton('Styled Button')\nbutton.setStyleSheet('''\nQPushButton {\n    background-color: #4CAF50;\n    border: none;\n    color: white;\n    padding: 10px 24px;\n    text-align: center;\n    font-size: 16px;\n    margin: 4px 2px;\n    border-radius: 8px;\n}\nQPushButton:hover {\n    background-color: #45a049;\n}\n''')
\n\n \n
← Python Pyqt Signals And SlotsSwagger Learning More β†’