PYQT Qcombobox将值选择为变量

时间:2015-10-02 16:22:15

标签: python pyqt

我有一个combox,并希望将框中选中的vaue添加到变量中。变量。我从文档中尝试了一些东西,并且只在将其设置为Qlabel时才成功。请帮忙

     self.languageLbl = QtGui.QLabel("Download_IVR", self)
     comboBox = QtGui.QComboBox(self)
     comboBox.addItem("IVR_ITALY")
     comboBox.addItem("IVR_FRANCE")
     comboBox.addItem("IVR_SPAIN")
     comboBox.addItem("IVR_GERMANY")
     comboBox.move(650, 250)
     comboBox.resize(150,40)
     self.languageLbl.move(650,150)
     comboBox.activated[str].connect(self.languageChoice)

 def download_button(self):

     ivrLang = self.comboBox.currentText()

我想将ivrLang设置为组合框中选择的项目。谢谢!

2 个答案:

答案 0 :(得分:1)

您没有将信号连接到回拨功能。你需要:

self.combobox.activated[str].connect(self.download_button)

下载按钮应如下所示:

def download_button(self, text):
    irvLang = text

请注意,您仍未使用该变量irvLang完成任何操作。

使用self创建类的comboBox和属性也是明智的:

self.comboBox = QtGui.QComboBox(self)

编辑: 这是一个完整的示例,它可以完成看起来想要的内容。

from PyQt4 import QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.cb = QtGui.QComboBox(self)
        self.cb.addItem("One")
        self.cb.addItem("Two")
        self.cb.activated[str].connect(self.selected)

    def selected(self, text):
        self.selected_text = text
        print self.selected_text

app = QtGui.QApplication([])
mw = MainWindow()
mw.show()
app.exec_()

答案 1 :(得分:0)

我最终将ivrLang设置为Qlabel。因此,当显示QLabel时,变量将设置为QLabel的文本。这样我就可以同时获得Label和变量。或许也许不是最好的方法,但它有效

    def languageChoice(self, text):

        self.languageLbl.setText(text)
    def download_button(self, text):
        directoryUser = self.directory
        ivrNum = self.lblNumber.text()
        username = self.userName.text()

        ivrLang = self.languageLbl.text()