PyQt5 QComboBox 样例代码

参考自:http://zetcode.com/gui/pyqt5/widgets2/

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

This example shows how to use
a QComboBox widget.

Original Author: Jan Bodnar
Website: zetcode.com
Modified by: Liu Qun
"""

from PyQt5.QtWidgets import (QApplication, QComboBox, QLabel, QVBoxLayout, QWidget)
import sys


class Example(QWidget):
    ITEMS = ['aaa', 'bbbb', 'ccccc']
    DEFAULT_ITEM = ITEMS[0]

    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout(self)
        self.combo = QComboBox(self)
        self.lbl = QLabel(self.DEFAULT_ITEM, self)
        self.setupUI()

    def setupUI(self):
        self.setWindowTitle('Title: QComboBox')
        self.layout.addWidget(self.combo)
        self.layout.addWidget(self.lbl)
        for i in self.ITEMS:
            self.combo.addItem(i)
        # noinspection PyUnresolvedReferences
        self.combo.activated.connect(self.onActivated)

    def onActivated(self):
        self.lbl.setText(self.combo.currentText())
        self.lbl.adjustSize()


def main(argv):
    app = QApplication(argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main(sys.argv)

你可能感兴趣的:(PyQt5 QComboBox 样例代码)