PyQt5常见布局、控件用法有哪些?

代码纪元 后端 2024-07-12

PyQt5常见布局、控件用法有哪些?

布局

  • QHBoxLayout - 水平布局。ini
代码解读
复制代码
tab1 = QtWidgets.QWidget() # 新建页面 tab1_layout = QtWidgets.QHBoxLayout() # 新建水平布局 tab1_layout.addWidget(button) # 向布局中添加单个控件 tab1.setLayout(tab1_layout) # 将水平布局添加到页面
  • QVBoxLayout - 垂直布局。ini
代码解读
复制代码
tab1 = QtWidgets.QWidget() # 新建页面 tab1_layout = QtWidgets.QVBoxLayout() # 新建垂直布局 tab1.setLayout(tab1_layout) # 将垂直布局添加到页面
  • QGridLayout - 在网格中组织小部件。python
代码解读
复制代码
self.tab1_gridLayout = QtWidgets.QGridLayout() # 新建网格布局 self.tab1_gridLayout.setHorizontalSpacing(30) # 设置水平间距 self.tab1_gridLayout.setVerticalSpacing(30) # 设置垂直间距 self.tab1_gridLayout.setContentsMargins(30, 30, 30, 30) # 设置内容边距(左,上,右,下) self.tab1_lable_pc_ip = QtWidgets.QLabel() self.tab1_gridLayout.addWidget(self.tab1_lable_pc_ip, 0, 0) # 将控件添加至布局 self.tab1_lineEdit_pc_ip = QtWidgets.QLineEdit() self.tab1_gridLayout.addWidget(self.tab1_lineEdit_pc_ip, 1, 0) tab1_layout.addLayout(self.tab1_gridLayout) # 将网格布局添加至水平/垂直布局
  • QFormLayout - 表单布局。ini
代码解读
复制代码
# 创建一个 QFormLayout 实例 layout = QFormLayout() # 添加一些字段到布局中 layout.addRow("姓名:", QLineEdit()) # 直接在 addRow 中创建 QLineEdit layout.addRow("年龄:", QLineEdit()) # 可以先创建控件,然后把它们作为参数传递给 addRow emailLabel = QLabel("邮箱:") emailField = QLineEdit() layout.addRow(emailLabel, emailField) acceptTermsCheckBox = QCheckBox("我接受条款和条件") layout.addRow("", acceptTermsCheckBox) # 对于复选框,左侧标签留空 submitButton = QPushButton("提交") layout.addRow("", submitButton) # 提交按钮,左侧标签留空 # 设置窗口的布局 self.setLayout(layout)

常用控件

  • QLabel - 显示文本或图片。scss
代码解读
复制代码
font = QFont() font.setPointSize(12) self.tab1_lable_name = QtWidgets.QLabel() self.tab1_lable_name.setText("name") # 设置lable名 self.tab1_lable_name.setFont(font) # 设置字体大小 layout.addWidget(self.tab1_lable_name) # 添加到布局
  • QPushButton - 命令按钮。python
代码解读
复制代码
self.PushButton_run = QPushButton("执行") self.PushButton_run.setFont(font) self.PushButton_run.setEnabled(False) self.PushButton_run.clicked.connect(self.run) layout.addWidget(self.PushButton_run) # 添加到布局 # 修改样式 self.button.setStyleSheet(""" QPushButton { border-radius: 20px; # 设置圆角的大小 background-color: lightblue; # 设置背景色 padding: 6px; # 设置内边距 } QPushButton:pressed { # 设置按钮被按下时的样式 background-color: darkblue; } """)
  • QRadioButton - 单选按钮。python
代码解读
复制代码
self.RadioButton_1 = QRadioButton('RadioButton_1') self.RadioButton_1.setChecked(True) # 默认选中 self.RadioButton_1.clicked.connect(self.onRadioButtonClicked) self.RadioButton_2 = QRadioButton('RadioButton_2') self.RadioButton_2.clicked.connect(self.onRadioButtonClicked) layout.addWidget(self.RadioButton_1) # 添加到布局 layout.addWidget(self.RadioButton_2) # 添加到布局 def onRadioButtonClicked(self): # 判断是哪个按钮被选中,并更新标签文本 radioButton = self.sender() if radioButton.isChecked(): self.label.setText(f"{radioButton.text()} 被选中")
  • QCheckBox - 多选框。python
代码解读
复制代码
self.cb1 = QCheckBox("选项 1") self.cb2 = QCheckBox("选项 2") self.cb1.stateChanged.connect(self.onCheckBoxClicked) self.cb2.stateChanged.connect(self.onCheckBoxClicked) layout.addWidget(self.cb1) layout.addWidget(self.cb2) def onCheckBoxClicked(self): # 初始化一个空字符串用以存放结果 result = "已选中:" # 检查复选框的状态并更新字符串 if self.cb1.isChecked(): result += "选项 1 " if self.cb2.isChecked(): result += "选项 2 "
  • QSlider - 滑块控件。
  • QProgressBar - 进度条。
  • QLineEdit - 单行文本输入框。python
代码解读
复制代码
self.lineEdit = QtWidgets.QLineEdit() self.lineEdit.setText("host_ip:") self.lineEdit.setFont(font) self.lineEdit.setPlaceholderText("输入IP,回车结束") # 设置提示文 self.lineEdit.textChanged.connect(self.onTextChanged) # 每当文本改变时,都会发出此信号 self.lineEdit.textEdited.connect(self.onTextChanged) #用户通过键入改变文本时,发出此信号(不包括通过编程方式更改文本) self.lineEdit.returnPressed.connect(self.onReturnPressed) # 当用户在该控件中按下回车(或确定)键时,发出此信号 layout.addWidget(self.lineEdit) # 添加控件到布局中 def onTextChanged(self, text): print(text) def onReturnPressed(self): print(self.lineEdit.text())
  • QTextEdit - 多行文本编辑控件。python
代码解读
复制代码
self.tab1_TextEdit_reply = QtWidgets.QTextEdit() self.tab1_TextEdit_reply.setText("reply:") self.tab1_TextEdit_reply.setFixedHeight(100) # 将高度设置为100像素 self.tab1_TextEdit_reply.textChanged.connect(self.onTextChanged) # 每当文本改变时,都会发出此信号 self.tab1_TextEdit_reply.cursorPositionChanged.connect(self.onTextChanged) #当光标位置改变时发出此信号 self.tab1_TextEdit_reply.selectionChanged.connect(self.onReturnPressed) # 当文本的选择区域变化时发出此信号
  • QComboBox - 下拉选择框。python
代码解读
复制代码
self.combo = QComboBox() # 向下拉列表中添加条目 self.combo.addItem('选项 1') self.combo.addItem('选项 2') self.combo.addItem('选项 3') self.combo.currentIndexChanged.connect(self.selectionChanged) # 当选项改变时触发的信号 def selectionChanged(self, i): text = self.combo.currentText() # 获取当前选中的文本并通过弹窗显示 print(f'选中的是: {text}')
  • QSpinBox - 数字输入框。
  • QDateEditQTimeEditQDateTimeEdit - 日期和时间输入控件。scss
代码解读
复制代码
self.data_now = QDateEdit() self.data_now.setDateTime(QtCore.QDateTime.currentDateTime())

高级控件

  • QTreeWidget - 显示树形结构数据。python
代码解读
复制代码
self.tree_widget = QtWidgets.QTreeWidget() # 设置列数和列标题 self.tree_widget.setColumnCount(1) self.tree_widget.setHeaderLabels(['Name']) self.root_item = QtWidgets.QTreeWidgetItem(self.tree_widget, "root_item_name") self.child_item = QtWidgets.QTreeWidgetItem(self.root_item, "child_item_name") self.root_item.addChild(self.child_item) # 可选的,因为子项已经通过构造函数添加 # 展开所有节点 self.tree_widget.expandAll() self.tree_widget.clicked.connect(self.tab2_tree_onClicked) splitter.addWidget(self.tree_widget) # 将树的布局 添加至QSplitter
  • QListWidget - 显示列表数据。
  • QTableWidget - 显示表格数据。python
代码解读
复制代码
self.table = QtWidgets.QTableWidget() # 设置行数和列数 self.table.setRowCount(4) # 4 行 self.table.setColumnCount(3) # 3 列 self.table.setColumnWidth(0, 100) # 为第一列设置宽度 100 self.table.setRowHeight(0, 50) # 为第一行设置高度 50 # 设置列标题 self.table.setHorizontalHeaderLabels(["列 1", "列 2", "列 3"]) self.table.setItem(0, 0, QtWidgets.QTableWidgetItem(str(device_ip)).setFont(font)) # 显示ip tab1_content_layout.addWidget(self.table) # 添加到布局

转载来源:https://juejin.cn/post/7365711904159105050

Apipost 私有化火热进行中

评论