I'm a complete beginner in PyQt and I put a combo box:

    select = QtGui.QComboBox(self)
    select.addItem("San Marcos")
    select.addItem("San Luis")
    select.addItem("San Lucas")
    select.addItem("Rosario I")
    select.addItem("Rosario II")

How I connect each item with an action. For example print sth in Terminal.

有帮助吗?

解决方案

Taken from here

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.lbl = QtGui.QLabel("Ubuntu", self)

        combo = QtGui.QComboBox(self)
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Red Hat")
        combo.addItem("Gentoo")

        combo.move(50, 50)
        self.lbl.move(50, 150)

        combo.activated[str].connect(self.onActivated)        

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QtGui.QComboBox')
        self.show()

    def onActivated(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()  

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top