Qt/PyQt5 강좌

pyQt5 QDoubleSpinBox 예제

webnautes 2024. 8. 11. 08:22
반응형

pyQt5 QDoubleSpinBox 예제입니다.



2024. 8. 11  최초작성



스핀박스를 클릭하여 값을 조정 후, 버튼을 누르면 스핀박스의 값이 출력됩니다. 

 




import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QDoubleSpinBox, QPushButton, QVBoxLayout, QTextEdit, QHBoxLayout

class CostSettingWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        main_layout = QHBoxLayout()
        left_layout = QVBoxLayout()
        grid = QGridLayout()

        items = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
        self.spinboxes = {}

        for i, item in enumerate(items):
            row = i // 3
            col = i % 3 * 2

            label = QLabel(item)
            spinbox = QDoubleSpinBox()
            spinbox.setRange(0, 1)
            spinbox.setSingleStep(0.1)
            spinbox.setValue(0.0)

            grid.addWidget(label, row, col)
            grid.addWidget(spinbox, row, col + 1)

            self.spinboxes[item] = spinbox

        left_layout.addLayout(grid)

        # 출력 버튼 추가
        self.print_button = QPushButton('값 출력')
        self.print_button.clicked.connect(self.print_values)
        left_layout.addWidget(self.print_button)

        # 텍스트 출력 영역 추가
        self.output_text = QTextEdit()
        self.output_text.setReadOnly(True# 읽기 전용으로 설정

        main_layout.addLayout(left_layout)
        main_layout.addWidget(self.output_text)

        self.setLayout(main_layout)
        self.setWindowTitle('QDoubleSpinBox')
        self.setGeometry(300, 300, 600, 300)

    def print_values(self):
        output = ""
        for item, spinbox in self.spinboxes.items():
            output += f"{item}: {spinbox.value()}\n"
        output += "-" * 20 + "\n"  # 구분선 추가
        self.output_text.append(output) 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = CostSettingWidget()
    ex.show()
    sys.exit(app.exec_())


반응형