Many widget

  • This example introduces various PyQt5 sub-widgets:

Listing 6 scripts/PYQT2/02_sub_widget/01_many_widget/main.py
  1# -*- coding: utf-8 -*-
  2
  3from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QLabel, QVBoxLayout, QWidget, QLineEdit,
  4                            QRadioButton, QComboBox, QCheckBox, QSlider, QTextEdit, QDateEdit, QTimeEdit,
  5                            QDateTimeEdit, QDial, QProgressBar)
  6
  7class MainWindow(QMainWindow):
  8    def __init__(self, parent=None):
  9        super(MainWindow, self).__init__(parent)
 10
 11        self.init_ui()
 12
 13    def init_ui(self):
 14        layout = QVBoxLayout()
 15
 16        # QLineEdit: 텍스트 입력 상자
 17        self.text_input = QLineEdit(self)
 18        self.text_input.setPlaceholderText("Type something here")
 19        layout.addWidget(self.text_input)
 20
 21        # QTextEdit: 멀티라인 텍스트 입력 상자
 22        self.text_edit = QTextEdit(self)
 23        layout.addWidget(self.text_edit)
 24
 25        # QRadioButton: 라디오 버튼
 26        self.radio_button1 = QRadioButton("Option 1", self)
 27        self.radio_button2 = QRadioButton("Option 2", self)
 28        layout.addWidget(self.radio_button1)
 29        layout.addWidget(self.radio_button2)
 30
 31        # QCheckBox: 체크박스
 32        self.checkbox = QCheckBox("Check me out!", self)
 33        layout.addWidget(self.checkbox)
 34
 35        # QComboBox: 드롭다운 메뉴
 36        self.combo_box = QComboBox(self)
 37        self.combo_box.addItems(["Choice 1", "Choice 2", "Choice 3"])
 38        layout.addWidget(self.combo_box)
 39
 40        # QSlider: 슬라이더
 41        self.slider = QSlider(self)
 42        layout.addWidget(self.slider)
 43
 44        # QDial: 다이얼
 45        self.dial = QDial(self)
 46        layout.addWidget(self.dial)
 47
 48        # QDateEdit, QTimeEdit, QDateTimeEdit: 날짜 및 시간 선택
 49        self.date_edit = QDateEdit(self)
 50        layout.addWidget(self.date_edit)
 51
 52        self.time_edit = QTimeEdit(self)
 53        layout.addWidget(self.time_edit)
 54
 55        self.datetime_edit = QDateTimeEdit(self)
 56        layout.addWidget(self.datetime_edit)
 57
 58        # QProgressBar: 진행 바
 59        self.progress_bar = QProgressBar(self)
 60        layout.addWidget(self.progress_bar)
 61
 62        # 버튼 추가: 사용자 입력 확인
 63        self.submit_button = QPushButton("Submit", self)
 64        layout.addWidget(self.submit_button)
 65
 66        # 사용자 입력 내용을 출력할 라벨 추가
 67        self.output_label = QLabel("", self)
 68        layout.addWidget(self.output_label)
 69
 70        # 버튼 클릭 시 실행될 슬롯(함수) 연결
 71        self.submit_button.clicked.connect(self.display_input)
 72
 73        # QVBoxLayout을 메인 윈도우에 설정
 74        central_widget = QWidget(self)
 75        central_widget.setLayout(layout)
 76        self.setCentralWidget(central_widget)
 77
 78    def display_input(self):
 79        text = self.text_input.text()
 80        text_edit_content = self.text_edit.toPlainText()
 81        option = self.radio_button1.text() if self.radio_button1.isChecked() else self.radio_button2.text()
 82        checkbox_status = "Checked" if self.checkbox.isChecked() else "Not Checked"
 83        choice = self.combo_box.currentText()
 84        slider_value = str(self.slider.value())
 85        dial_value = str(self.dial.value())
 86        date_value = self.date_edit.date().toString()
 87        time_value = self.time_edit.time().toString()
 88        datetime_value = self.datetime_edit.dateTime().toString()
 89
 90        output = ("Text Input: %s\n"
 91                "Text Edit: %s\n"
 92                "Selected Option: %s\n"
 93                "Checkbox Status: %s\n"
 94                "Selected Choice: %s\n"
 95                "Slider Value: %s\n"
 96                "Dial Value: %s\n"
 97                "Date: %s\n"
 98                "Time: %s\n"
 99                "DateTime: %s") % (text, text_edit_content, option, checkbox_status, choice, slider_value, dial_value, date_value, time_value, datetime_value)
100
101        self.output_label.setText(output)
102
103if __name__ == "__main__":
104    app = QApplication([])
105    mainWindow = MainWindow()
106    mainWindow.show()
107    app.exec_()
  • QLineEdit: A widget for inputting a single line of text.

  • QTextEdit: A widget for inputting multiple lines of text.

  • QRadioButton: A widget that allows the user to choose one of many options.

  • QCheckBox: A checkbox widget indicating whether it’s checked or not.

  • QComboBox: A widget allowing selection from a dropdown list.

  • QSlider and QDial: Widgets to slide or turn to select values.

  • QDateEdit, QTimeEdit, QDateTimeEdit: Widgets to select date, time, or both.

  • QProgressBar: A graphical representation of progress.

These sub-widgets are used to receive various types of input from users or to present information. This example demonstrates the basic functionalities and how each widget is used.