分组框控件学习03

68 阅读1分钟

尝试复现下面的图片

image.png

from PyQt6.QtWidgets import QApplication, QWidget, QGridLayout, QGroupBox, QVBoxLayout, QFormLayout, QHBoxLayout, \
    QLineEdit, QPushButton, QLabel, QSpinBox, QDoubleSpinBox
import sys

class MainWindows(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui()

    def ui(self):
        self.setWindowTitle("Demo3,1数值输入")
        self.resize(200, 300)
        self.show()
        ####################################
        # 创建布局
        ####################################
        self.qline_edit_groupbox = QGroupBox("QLineEdit输入和显示数值")
        self.qline_edit_groupbox.setStyleSheet("QGroupBox::title{font-size:16px;color:red;}")  # 设置字体大小和颜色
        self.spine_groupbox = QGroupBox("SpinBox输入和显示")
        self.spine_groupbox.setStyleSheet("QGroupBox::title{font-size:16px;color:red;}")
        main_layout = QVBoxLayout()

        ####################################
        # 控件1
        ####################################
        line_edit_h = QHBoxLayout()
        self.label1 = QLabel("数量")
        self.line_edit1 = QLineEdit()
        self.label2 = QLabel("单价")
        self.line_edit2 = QLineEdit()
        line_edit_h.addWidget(self.label1)
        line_edit_h.addWidget(self.line_edit1)
        line_edit_h.addWidget(self.label2)
        line_edit_h.addWidget(self.line_edit2)

        line_edit_h2 = QHBoxLayout()
        self.button1 = QPushButton("计算总价")
        self.label3 = QLabel("总价")
        self.line_edit3 = QLineEdit()
        line_edit_h2.addWidget(self.button1)
        line_edit_h2.addWidget(self.label3)
        line_edit_h2.addWidget(self.line_edit3)

        v_layout1 = QVBoxLayout()
        v_layout1.addLayout(line_edit_h)
        v_layout1.addLayout(line_edit_h2)
        self.qline_edit_groupbox.setLayout(v_layout1)

        ####################################
        # 控件2
        ####################################
        self.label4 = QLabel("数量")
        self.spin_box = QSpinBox()  # 只支持整数
        self.spin_box.setValue(16)  # 设置初始值
        self.spin_box.setMinimum(0)  # 设置最小值
        self.spin_box.setMaximum(100)  # 设置最大值
        self.spin_box.setSingleStep(1)  # 设置步长
        self.spin_box.setSuffix(" kg")  # 设置单位

        self.label5 = QLabel("单价")
        self.spin_box2 = QDoubleSpinBox()  # 支持浮点数
        self.spin_box2.setValue(11.23)
        self.spin_box2.setPrefix("$ ")
        h_layout = QHBoxLayout()
        h_layout.addWidget(self.label4)
        h_layout.addWidget(self.spin_box)
        h_layout.addWidget(self.label5)
        h_layout.addWidget(self.spin_box2)

        self.label6 = QLabel("自动计算总价")
        self.spin_box3 = QDoubleSpinBox()
        self.spin_box3.setPrefix("$ ")
        h_layout1 = QHBoxLayout()
        h_layout1.addWidget(self.label6)
        h_layout1.addWidget(self.spin_box3)

        v_layout2 = QVBoxLayout()
        v_layout2.addLayout(h_layout)
        v_layout2.addLayout(h_layout1)
        self.spine_groupbox.setLayout(v_layout2)

        main_layout.addWidget(self.qline_edit_groupbox)
        main_layout.addWidget(self.spine_groupbox)
        self.setLayout(main_layout)

        ####################################
        # 信号和槽
        ####################################
        self.button1.clicked.connect(self.button1_function)
        self.spin_box.valueChanged.connect(self.spin_box_function)
        self.spin_box2.valueChanged.connect(self.spin_box_function)

    def button1_function(self):
        if self.line_edit1.text() and self.line_edit2.text():
            value1 = float(self.line_edit1.text())
            value2 = float(self.line_edit2.text())
            all_price = value1 * value2
            self.line_edit3.setText(str(all_price))

    def spin_box_function(self, value):
        if self.sender() == self.spin_box:
            value1 = value  # value 是 int 类型
            value2 = self.spin_box2.value()  # 获取 self.spin_box2 的值(float 类型)
        elif self.sender() == self.spin_box2:
            value1 = self.spin_box.value()  # 获取 self.spin_box 的值(int 类型)
            value2 = value  # value 是 float 类型
        else:
            return  # 如果信号来源不明,直接返回

        all_price = value1 * value2
        self.spin_box3.setValue(all_price)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindows()
    sys.exit(app.exec())

代码运行效果

Kapture 2025-05-09 at 15.20.05.gif