ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Day 6. PyQt5 (QSpinBox, QSlider, QDial)
    <개인공부> - IT/[Python] 2021. 3. 18. 00:10
    반응형

    QSpinBoxQDoubleSpinBox는 숫자를 입력할 수 있는 input box와 함께 증가, 감소를 적용할 수 있는 화살표가 존재하는 위젯이다. 두 가지는 동일한 기능을 하며 정수형 value를 사용하게 되면 QSpinBox를 실수형은 QDoubleSpinBox를 사용하면 된다.

    import sys
    
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QMainWindow, QSpinBox
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("My App")
    
            widget = QSpinBox()
            # Or: widget = QDoubleSpinBox()
    
            widget.setMinimum(-10)
            widget.setMaximum(3)
            # Or: widget.setRange(-10, 3)
    
            widget.setPrefix("$")
            widget.setSuffix("c")
            widget.setSingleStep(3) #Or e.g. 0.5 for QDoubleSpinBox
            widget.valueChanged.connect(self.value_changed)
            widget.valueChanged[str].connect(self.value_changed_str)
    
            self.setCentralWidget(widget)
    
        def value_changed(self, i):
            print(i)
        def value_changed_str(self, s):
            print(s)
    
    app = QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    
    app.exec_()

     

    실행화면

    QSpinBox와 QDoubleSpinBox 모두 값이 변경되면 .valueChanged라는 signal이 발생한다. 단순히 해당 시그널은 numeric value를 보내게 되고 str 키워드를 이용하게 되면 signal로 발생되는 값은 string으로 보내진다.


    QSlider는 slider-bar 위젯이다. 따라서, QSpinBox와 유사하다고 볼 수 있다. 다시 말해서 현재 value를 숫자의 형태로 출력하는 QSpinBox의 형태가 Qlider에서는 slider의 위치로 현재 값을 대표한다. Volume을 조절하는 컨트롤러를 생각해보면 매우 정확한 정확도는 필요없으나 작다 크다의 범위 내에서 조절이 필요한 경우 QSlider가 많이 이용된다. 

    import sys
    
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QMainWindow, QSlider
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("My App")
    
            widget = QSlider(Qt.Horizontal)
            # Or: widget = Qlist(), Vertical slider
            
            widget.setMinimum(-10)
            widget.setMaximum(3)
            # Or: widget.setRange(-10, 3)
    
            widget.setSingleStep(3) #Or e.g. 0.5 for QDoubleSpinBox
            widget.valueChanged.connect(self.value_changed)
            widget.sliderMoved.connect(self.slider_position)
            widget.sliderPressed.connect(self.slider_pressed)
            widget.sliderReleased.connect(self.slider_released)
    
            self.setCentralWidget(widget)
    
        def value_changed(self, i):
            print(i)
        def slider_position(self, p):
            print("position", p)
        def slider_pressed(self):
            print("Pressed!")
        def slider_released(self):
            print("Released")
    
    app = QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    
    app.exec_()

     

     참고로 QSlider를 생성할 때 별도의 Qt namespace의 flag를 지정하지 않으면 수직방향의 슬라이더가 생성된다. 

     


    QDial은 우리가 잘 알고 있는 오디오 장비의 아날로그 다이얼을 생각하면 된다. 역시나 QSlier와 유사한 기능을 갖고있고 표현방식의 차이를 갖는 위젯이다. 좌우 또는 위아래로 값을 변경하는 것이 아니라 아날로그 다이얼처럼 빙글빙글 돌려서 값을 조절한다. QDial에서 갖고있는 signal이름은 QSlier와 동일하다.

     

    import sys
    
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication, QMainWindow, QDial
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("My App")
    
            widget = QDial()
            widget.setRange(-10, 100)
            widget.setSingleStep(0.5)
            
            widget.valueChanged.connect(self.value_changed)
            widget.sliderMoved.connect(self.slider_position)
            widget.sliderPressed.connect(self.slider_pressed)
            widget.sliderReleased.connect(self.slider_released)
    
            self.setCentralWidget(widget)
    
        def value_changed(self, i):
            print(i)
        def slider_position(self, p):
            print("position", p)
        def slider_pressed(self):
            print("Pressed!")
        def slider_released(self):
            print("Released")
    
    app = QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    
    app.exec_()

     

     

    1. Reference book: "Create GUI Applications with Python & Qt5: The hands-on guide to making apps with Python"

    반응형

    '<개인공부> - IT > [Python]' 카테고리의 다른 글

    Day 8. PyQt5 (Actions, Toolbars, Menus)  (0) 2021.03.20
    Day 7. PyQt5 (Layouts)  (0) 2021.03.19
    Day 5. PyQt5 (QListWidget, QLineEdit)  (0) 2021.03.10
    Day 4. PyQt5 (QCheckBox, QComboBox)  (0) 2021.03.05
    Day 3. PyQt5 (QLabel)  (0) 2021.02.09
Designed by Tistory.