Python 프로그래밍 > PyQt5 - QtWidgets - QComboBox (콤보박스 예제)
등록일 : 2018-07-11 16:09
조회수 : 94,422
이번에는 콤보박스 예제 입니다.
콤보박스를 추가하고, 선택시에 Label 에 선택된 값을 출력해 주는 예제 입니다.
# -*- coding: utf-8 -*-
import sys
import codecs
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMenuBar, QAction, QLabel, QComboBox, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel(self)
self.lbl.move(50, 50)
combo = QComboBox(self)
combo.addItem("독수리", "eagle")
combo.addItem("강아지", "puppy")
combo.addItem("고양이", "cat")
combo.move(50, 100)
combo.activated.connect(self.changeCombo)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter')
self.show()
def changeCombo(self, text):
sender = self.sender()
idx = sender.currentIndex()
self.lbl.setText(sender.itemData(idx))
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())