Python PyQt Common Widgets
PyQt is a GUI programming toolkit for the Python language, and it is the Python binding for the Qt application framework.
Qt is a cross-platform C++ GUI application development framework, widely used for developing GUI programs.
PyQt provides a rich set of widgets to build user interfaces; these widgets are the basic building blocks for constructing GUI applications.
Basic Window Widgets
QMainWindow
QMainWindow is the main window class, providing a standard application framework:
Example
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 for all user interface objects:
Example
widget = QWidget()
widget.setWindowTitle('Basic Window')
widget.show()
Common Input Widgets
QLabel
Display text or images:
Example
label = QLabel('This is a label')
label.setStyleSheet('font-size: 16px; color: blue;')
QLineEdit
Single-line text input box:
Example
line_edit = QLineEdit()
line_edit.setPlaceholderText('Please enter content...')
QTextEdit
Multi-line text editor:
Example
text_edit = QTextEdit()
text_edit.setPlainText('Multi-line Text Editor')
QComboBox
Drop-down selection box:
Example
combo = QComboBox()
combo.addItems(['Option 1', 'Option 2', 'Option 3'])
Button Widgets
QPushButton
Normal button:
Example
button = QPushButton('Click me')
button.setStyleSheet('background-color: green; color: white;')
QRadioButton
Radio button:
Example
radio1 = QRadioButton('Option 1')
radio2 = QRadioButton('Option 2')
QCheckBox
Checkbox:
Example
check1 = QCheckBox('Choice 1')
check2 = QCheckBox('Choice 2')
Layout Widgets
QHBoxLayout
Horizontal layout:
Example
h_layout = QHBoxLayout()
h_layout.addWidget(QPushButton('Left'))
h_layout.addWidget(QPushButton('Center'))
h_layout.addWidget(QPushButton('Right'))
QVBoxLayout
Vertical layout:
Example
v_layout = QVBoxLayout()
v_layout.addWidget(QPushButton('Top'))
v_layout.addWidget(QPushButton('Middle'))
v_layout.addWidget(QPushButton('Bottom'))
QGridLayout
Grid layout:
Example
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
table = QTableWidget(3, 3) # 3 rows 3 columns
table.setHorizontalHeaderLabels(['Column 1', 'Column 2', 'Column 3'])
table.setItem(0, 0, QTableWidgetItem('Data 1'))
QTreeWidget
Tree widget:
Example
tree = QTreeWidget()
tree.setHeaderLabels(['Name', 'Value'])
root = QTreeWidgetItem(tree, ['Root Node'])
child = QTreeWidgetItem(root, ['Child Node', '123'])
QTabWidget
Tab widget:
Example
tab = QTabWidget()
tab.addTab(QWidget(), 'Tab 1')
tab.addTab(QWidget(), 'Tab 2')
Dialog Widgets
QMessageBox
Message dialog:
Example
msg = QMessageBox()
msg.setWindowTitle('Prompt')
msg.setText('This is a message dialog')
msg.setIcon(QMessageBox.Information)
msg.exec_()
QFileDialog
File dialog:
Example
file_name = QFileDialog.getOpenFileName(None, 'Select File', '', 'Text Files (*.txt);;All Files (*)')
Style Settings
PyQt supports using CSS styles to beautify widgets:
Example
button.setStyleSheet('''
QPushButton {
background-color: #4C7EAF;
border: none;
color: white;
padding: 10px 24px;
text-align: center;
font-size: 16px;
margin: 4px 2px;
border-radius: 8px;
}
QPushButton:hover {
background-color: #4777A6;
}
''')