PYQT Qcombobox 将选定的值设置为变量 [英] PYQT Qcombobox set value selected to a variable
问题描述
我有一个组合框,想将框中选择的值添加到变量中.变量.我从文档中尝试了一些东西,并且仅在将其设置为 Qlabel 时才成功.任何帮助请
I have a combox, and want to add the vaue selected in the box to a variable. The variable. I tried a few things from the documentation and was only successful on setting it to a Qlabel. Any help please
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 设置为组合框中选择的项目.谢谢!
I want to set ivrLang to the item selected in the combobox. Thanks!
推荐答案
您没有将信号连接到回调函数.您需要:
You aren't connecting your signal to your callback function. You need:
self.combobox.activated[str].connect(self.download_button)
下载按钮应如下所示:
def download_button(self, text):
irvLang = text
请注意,您还没有对那个变量 irvLang
做任何事情.
Note that you still haven't done anything with that variable irvLang
.
此外,使用 self
制作类的组合框和属性也是明智的:
Also it would be wise to make the comboBox and attribute of your class using self
:
self.comboBox = QtGui.QComboBox(self)
这是一个完整的示例,可以满足您似乎想要的功能.
Here is a complete example that does what you seem to want.
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_()
这篇关于PYQT Qcombobox 将选定的值设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!