PYQT Qcombobox 将选择的值设置为变量

PYQT Qcombobox set value selected to a variable

我有一个组合框,想将在框中选择的值添加到一个变量中。变量。我尝试了文档中的一些内容,但只成功地将其设置为 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 设置为在组合框中选择的项目。谢谢!

您没有将信号连接到回调函数。您需要:

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

下载按钮应如下所示:

def download_button(self, text):
    irvLang = text

请注意,您还没有对该变量进行任何操作 irvLang

此外,使用 self:

制作 class 的组合框和属性也是明智的
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_()

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

    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()