python - 如何从 PyQt4 中的 ComboBox 获取输入

标签 python python-2.7 python-3.x pyqt pyqt4

我在 PyQt4 中创建了一个组合框。该组合框将有 5 个选项,用户需要选择其中一个选项,然后单击提交按钮。我试图定义一个名为打印操作的函数,在用户单击提交按钮后使用

def home(self):

    self.lbl = QtGui.QLabel('Types of Analysis', self)
    self.lbl.setFont(QtGui.QFont('SansSerif', 15))
    btn = QtGui.QPushButton('Submit', self)
    btn.move(200, 200)
    cb = QtGui.QComboBox(self)
    btn = QtGui.QPushButton('Submit', self)
    cb.addItem('Sentiment Analysis')
    cb.addItem('Data Cleansing')
    cb.addItem('Genomics')
    cb.addItem('Integration')
    cb.addItem('Visualization')
    cb.move(200,100)
    cb.resize(150,40)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
    cb.activated[str].connect(self.onactivate)
    btn.clicked.connect(self.printingaction)
    self.show()

def printingaction(self):
    print(t)

您能否帮助我了解在用户选择给定选项之一并按下提交按钮后如何接受输入

最佳答案

这是一个示例,它将打印组合框的当前文本和索引:

import sys
from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent = None):
        super(MyWindow, self).__init__(parent)

        # Create controls
        self.lbl = QtGui.QLabel('Types of Analysis', self)
        self.lbl.setFont(QtGui.QFont('SansSerif', 15) )
        self.cb = QtGui.QComboBox(self)
        self.cb.addItems(['Sentiment Analysis', 'Data Cleansing', 'Genomics', 'Integration', 'Visualization'])
        self.btn = QtGui.QPushButton('Submit', self)
        self.btn.clicked.connect(self.printingaction)

        # Create layout
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.lbl)
        mainLayout.addWidget(self.cb)
        mainLayout.addWidget(self.btn)
        self.setLayout(mainLayout)

        self.show()

    def printingaction(self):
        print 'Current item: {0}'.format( self.cb.currentIndex() ) # ComboBox's index
        print 'Current index: {0}'.format( self.cb.currentText() ) # ComboBox's text

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    sys.exit( app.exec_() )

关于python - 如何从 PyQt4 中的 ComboBox 获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196391/

相关文章:

python - 为什么包含 1472 字节文件的变量的字节大小不同

python - 为什么python线程会减慢推理时间以实现更快的R-CNN

python - 如何从 botocore 捕获异常

python - 如何替换换行符,除非字符出现在 Python 3 的该行中

python-3.x - 属性错误 : type object 'h5py.h5.H5PYConfig' has no attribute '__reduce_cython__'

python - 使用 scikit-learn 的一次性编码

python - 一种快速连续多次将python中的数百万项传递给C程序的方法

python - 我可以从函数参数中拆分一个数组吗

Python 限制 readlines() 的换行符

python - 如何在内置输出上调用 python 声音设备包