PyQt QComboBox サンプル

Tutorialからだいたいそのまま

f:id:togari_takamoto:20120312162734p:plain

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        combo = QtGui.QComboBox(self)
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Red Hat")
        combo.addItem("Gentoo")

        combo.activated.connect(self.onActivated)
        combo.activated[str].connect(self.onActivatedstr)
        combo.move(10, 10)
        self.move(300, 300)
        self.setWindowTitle('QtGui.QComboBox')
        self.show()
    def onActivated(self,number):
        print(number)#0,1,2,3,4
    def onActivatedstr(self,text):
        print(text)#Ubuntu,Mandriva,Fedora,Red Hat,Gentoo
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()