[PyQt5] 6. Widgets
Qt5에는 다음과 같이 다양한 위젯이 있습니다.
다행히 아이콘만 보더라도
대부분의 위젯이 어떤 용도로 사용되는지
알 수 있습니다.
비슷비슷한 위젯을
비슷비슷한 코드로 나열하는 것은
지루한 일입니다.
공식 문서와 구글링 및 Qt Designer 사용을 추천드리고,
여기서는 제가 자주 쓰는 위젯만 간단히 나열하겠습니다.
- QLabel: 텍스트, 리치 텍스트, 그림, 영상 등을 구현할 수 있음.
- QLineEdit: 원 라인 에디터, 패스워드 가능.
- QTextEdit: 멀티 라인 에디터, 리치 텍스트 지원.
- QGroupBox: 그룹 박스, 프레임과 제목을 지원하는 컨테이너.
- QCheckBox: 체크박스
- QPushButton: 푸시 버튼
- QRadioButton: 라디오 버튼
- QListBox: 리스트 박스
- QComboBox: 콤보 박스
- QProgressBar: 프로그레스 바
- QSlider: 슬라이스 바
- QMenu: 메뉴
- QPopupMenu 팝업 메뉴
여기서 딱 끝내고 싶지만.. 그래도...
Qlabel
앞서 사용했던 위젯입니다.
공식문서의 Detailed Description를 읽어봅시다.
https://doc.qt.io/qt-5/qlabel.html
QLabel은 텍스트나 이미지를 표시하는 데 사용된다.
사용자 상호작용 기능은 제공되지 않는다.
라고 합니다.
일반 텍스트, 리치 텍스트(HTML의 Tag도 사용할 수 있습니다.),
픽스 맵, 영화, 숫자 등의 컨텐츠를 담을 수 있네요.
퍼블릭 슬롯에 setText()가 보입니다.
label에 text를 쓸 때 사용하는 메서드죠.
그 위엔 setNum, setMovie, setPicture 등등이 있는데...
어떻게 쓸 수 있을 지 느낌이 옵니다.
QLineEdit
하나의 라인을 편집할 수 있는 위젯입니다. (멀티 라인 편집은 QTextEdit)
자주 사용되는 메서드로는
텍스트를 쓰는 setText(),
텍스트를 읽는 text(),
배경에 텍스트를 표시하는 setPlaceholderText(),
읽기 전용으로 바꿔주는 setReadOnly(),
주로 암호 입력에 사용되는 setEchoMode() 등이 있습니다.
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.line_edit1 = QLineEdit('')
self.line_edit1.setPlaceholderText('ID')
self.line_edit1.textChanged.connect(self.line_edit_text_changed)
self.line_edit2 = QLineEdit('')
self.line_edit2.setPlaceholderText('PASSWORD')
self.line_edit2.setEchoMode(QLineEdit.Password)
self.line_edit2.returnPressed.connect(self.line_edit_return_pressed)
self.label = QLabel('label')
layout = QVBoxLayout()
layout.addWidget(self.line_edit1)
layout.addWidget(self.line_edit2)
layout.addWidget(self.label)
self.setLayout(layout)
def line_edit_text_changed(self):
self.label.setText(self.line_edit1.text())
def line_edit_return_pressed(self):
self.label.setText(self.line_edit2.text())
app = QApplication([])
my_window = MyWindow()
my_window.show()
app.exec_()
https://github.com/pycrawling/pyqt5_study/blob/main/pyqt5_study_06-1.py
QTextEdit
QTextEdit와 유사한 QPlainTextEdit도 있습니다.
플레인 텍스트와 리치 텍스트는 반대말이죠?
QTextEdit는 리치 텍스트도 지원하고,
QPlainTextEdit는 플레인 텍스트만 지원합니다.
용도에 맞춰 사용하시면 됩니다.
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel('label')
self.text_edit = QTextEdit('Text Edit')
self.text_edit.setPlaceholderText('ID')
self.button = QPushButton('Enter')
self.button.clicked.connect(self.button_clicked)
grid = QVBoxLayout()
grid.addWidget(self.label)
grid.addWidget(self.text_edit)
grid.addWidget(self.button)
self.setLayout(grid)
def button_clicked(self):
self.label.setText(self.text_edit.toPlainText())
app = QApplication([])
my_window = MyWindow()
my_window.show()
app.exec_()
https://github.com/pycrawling/pyqt5_study/blob/main/pyqt5_study_06-2.py
QGroupBox, QRadioButton
그룹 박스는 컨테이너기 때문에
레이아웃을 먼저 만들어야 합니다.
먼저 레이아웃에 위젯들을 넣어준 뒤
그룹 박스에 레이아웃을 set 합니다.
라디오 버튼은 배타적인 방식으로 작동합니다.
하나의 레이아웃 안에서는 1개만 선택할 수 있습니다.
이를 풀 수 있는 방법도 있지만
제 경험 상 풀어야 할 필요를 느낀 일은 없었습니다.
clicked 대신 toggled를 쓸 수도 있지만
toggled와 setChecked를 같이 쓰면
에러가 발생할 수도 있습니다.
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
groupbox = QGroupBox('성별')
groupbox.setCheckable(True)
groupbox.setChecked(True)
self.button0 = QRadioButton('남')
self.button0.clicked.connect(self.button_clicked)
self.button1 = QRadioButton('녀')
self.button1.clicked.connect(self.button_clicked)
self.button2 = QRadioButton('둘 다 아님')
self.button2.clicked.connect(self.button_clicked)
self.button3 = QRadioButton('밝힐 수 없다')
self.button3.clicked.connect(self.button_clicked)
self.button3.setChecked(True)
self.line_edit = QLineEdit('')
box_layout = QVBoxLayout()
box_layout.addWidget(self.button0)
box_layout.addWidget(self.button1)
box_layout.addWidget(self.button2)
box_layout.addWidget(self.button3)
groupbox.setLayout(box_layout)
layout = QVBoxLayout()
layout.addWidget(groupbox)
layout.addWidget(self.line_edit)
self.setLayout(layout)
def button_clicked(self):
if self.button0.isChecked():
self.line_edit.setText(self.button0.text())
elif self.button1.isChecked():
self.line_edit.setText(self.button1.text())
elif self.button2.isChecked():
self.line_edit.setText(self.button2.text())
elif self.button3.isChecked():
self.line_edit.setText(self.button3.text())
app = QApplication([])
my_window = MyWindow()
my_window.show()
app.exec_()
https://github.com/pycrawling/pyqt5_study/blob/main/pyqt5_study_06-3.py
QCheckBox
stateChanged 대신 clicked 쓸 수도 있습니다.
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
groupbox = QGroupBox('가지고 싶은 필기구')
groupbox.setCheckable(True)
groupbox.setChecked(True)
self.button0 = QCheckBox('연필')
self.button0.stateChanged.connect(self.button_changed)
self.button1 = QCheckBox('지우개')
self.button1.stateChanged.connect(self.button_changed)
self.button2 = QCheckBox('볼펜')
self.button2.stateChanged.connect(self.button_changed)
self.button3 = QCheckBox('샤프')
self.button3.stateChanged.connect(self.button_changed)
self.line_edit = QLineEdit('')
box_layout = QVBoxLayout()
box_layout.addWidget(self.button0)
box_layout.addWidget(self.button1)
box_layout.addWidget(self.button2)
box_layout.addWidget(self.button3)
groupbox.setLayout(box_layout)
layout = QVBoxLayout()
layout.addWidget(groupbox)
layout.addWidget(self.line_edit)
self.setLayout(layout)
def button_changed(self):
message = []
if self.button0.isChecked():
message.append(self.button0.text())
if self.button1.isChecked():
message.append(self.button1.text())
if self.button2.isChecked():
message.append(self.button2.text())
if self.button3.isChecked():
message.append(self.button3.text())
self.line_edit.setText(', '.join(message))
app = QApplication([])
my_window = MyWindow()
my_window.show()
app.exec_()
https://github.com/pycrawling/pyqt5_study/blob/main/pyqt5_study_06-4.py
더 보면 좋겠지만...
위젯은 여기까지만...