从组合框中获取所选选项

时间:2014-10-15 18:38:33

标签: python qt combobox pyqt4

这是我使用pyuic4从NetSim.ui转换为NetSim.py的python代码。

组合框名称更改为编解码器。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'NetSim.ui'
#
# Created: Wed Oct 15 11:46:03 2014
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
   _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
   def _fromUtf8(s):
      return s

try:
   _encoding = QtGui.QApplication.UnicodeUTF8
   def _translate(context, text, disambig):
      return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
   def _translate(context, text, disambig):
      return QtGui.QApplication.translate(context, text, disambig)

class Ui_NetSim(object):
   def setupUi(self, NetSim):
      NetSim.setObjectName(_fromUtf8("NetSim"))
      NetSim.resize(641, 564)
      self.Codecs = QtGui.QComboBox(NetSim)
      self.Codecs.setGeometry(QtCore.QRect(270, 90, 81, 29))
      self.Codecs.setObjectName(_fromUtf8("Codecs"))
      self.Codecs.addItem(_fromUtf8(""))
      self.Codecs.addItem(_fromUtf8(""))
      self.Codecs.addItem(_fromUtf8(""))
      self.Codecs.addItem(_fromUtf8(""))
      self.Codecs.addItem(_fromUtf8(""))
      self.Codecs.addItem(_fromUtf8(""))

   def retranslateUi(self, NetSim):

      NetSim.setWindowTitle(_translate("NetSim", "Dialog", None))
      self.Codecs.setItemText(0, _translate("NetSim", "Codecs", None))
      self.Codecs.setItemText(1, _translate("NetSim", "AMR-WB", None))
      self.Codecs.setItemText(2, _translate("NetSim", "AMR-WB+", None))
      self.Codecs.setItemText(3, _translate("NetSim", "VMR-WB", None))
      self.Codecs.setItemText(4, _translate("NetSim", "EVRC", None))
      self.Codecs.setItemText(5, _translate("NetSim", "EVRCb", No

我正在使用另一个python脚本编写自己的函数并将其连接到小部件。我想得到组合框的选定值并使用它(比如打印它)但它在代码中显示一个红叉我希望获得Codec的选定值,我无法找到错误。是: val = str(Ui_NetSim.Codecs.currentText()) AttributeError:type object' Ui_NetSim'没有属性'编解码器' 这是:

import sys

from PyQt4 import QtCore, QtGui
from NetSim import Ui_NetSim
from PyQt4.Qt import QString
from __builtin__ import str

class AppGui(QtGui.QDialog,Ui_NetSim):

   def __init__(self):
      QtGui.QDialog.__init__(self)

      self.ui = Ui_NetSim()
      self.ui.setupUi(self)

      self.ui.Codecs.activated.connect(self.select_Codec)

    def select_Codec(self):

     val = str(Ui_NetSim.Codecs.currentText())
     print val


app = QtGui.QApplication(sys.argv)
window = AppGui()
ui = Ui_NetSim()
window.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您正在尝试访问类Codecs的类变量Ui_NetSim。但是,CodecsUi_NetSim的实例属性,因此仅适用于类Ui_NetSim的实例。您应该通过self.ui.Codecs.currentText()

访问它

所以你的方法变成了:

def select_Codec(self):
    val = str(self.ui.Codecs.currentText())
    print val

您的程序底部似乎还有一个额外的ui = Ui_NetSim()

window = AppGui()
ui = Ui_NetSim() # Delete this line, it is doing nothing!
window.show()