Python3.x Python PyQt Common Widgets
\n\nPyQt is a GUI programming toolkit for the Python language. It is a Python binding for the Qt application framework.
\n\nQt is a cross-platform C++ graphical user interface application development framework, widely used for developing GUI programs.
\n\nPyQt 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\nQMainWindow
\n\nQMainWindow is the main window class, providing a standard application framework:
\n\nExample
\n\nfrom 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\nQWidget
\n\nQWidget is the base class for all user interface objects:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QWidget\n\nwidget = QWidget()\nwidget.setWindowTitle('Basic Window')\nwidget.show()\n\n\n\n
Common Input Components
\n\nQLabel
\n\nDisplays text or images:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QLabel\n\nlabel = QLabel('This is a label')\nlabel.setStyleSheet('font-size: 16px; color: blue;')\n\nQLineEdit
\n\nSingle-line text input box:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QLineEdit\n\nline_edit = QLineEdit()\nline_edit.setPlaceholderText('Please enter content...')\n\nQTextEdit
\n\nMulti-line text editor:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QTextEdit\n\ntext_edit = QTextEdit()\ntext_edit.setPlainText('Multi-line Text Editor')\n\nQComboBox
\n\nDrop-down selection box:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QComboBox\n\ncombo = QComboBox()\ncombo.addItems(['Option 1','Option 2','Option 3'])\n\n\n\n
Button Components
\n\nQPushButton
\n\nStandard button:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QPushButton\n\nbutton = QPushButton('Click Me')\nbutton.setStyleSheet('background-color: green; color: white;')\n\nQRadioButton
\n\nRadio button:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QRadioButton\n\nradio1 = QRadioButton('Option 1')\nradio2 = QRadioButton('Option 2')\n\nQCheckBox
\n\nCheckbox:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QCheckBox\n\ncheck1 = QCheckBox('Choice 1')\ncheck2 = QCheckBox('Choice 2')\n\n\n\n
Layout Components
\n\nQHBoxLayout
\n\nHorizontal layout:
\n\nExample
\n\nfrom 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\nQVBoxLayout
\n\nVertical layout:
\n\nExample
\n\nfrom 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\nQGridLayout
\n\nGrid layout:
\n\nExample
\n\nfrom 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\nQTableWidget
\n\nTable component:
\n\nExample
\n\nfrom 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'))QTreeWidget
\n\nTree component:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem\n\ntree = QTreeWidget()\ntree.setHeaderLabels(['Name','Value'])\nroot = QTreeWidgetItem(tree,['Root Node'])\nchild = QTreeWidgetItem(root,['Child Node','123'])\n\nQTabWidget
\n\nTab component:
\n\nExample
\n\nfrom 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\nQMessageBox
\n\nMessage dialog:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QMessageBox\n\nmsg = QMessageBox()\nmsg.setWindowTitle('Prompt')\nmsg.setText('This is a message dialog')\nmsg.setIcon(QMessageBox.Information)\nmsg.exec_()\n\nQFileDialog
\n\nFile dialog:
\n\nExample
\n\nfrom PyQt5.QtWidgets import QFileDialog\n\nfile_name = QFileDialog.getOpenFileName(None,'Select File','','Text Files (*.txt);;All Files (*)')\n\n\n\n
Styling
\n\nPyQt supports using CSS styles to beautify components:
\n\nExample
\n\nbutton = 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
YouTip