简体   繁体   English

如何从PyQt4中的ComboBox接受输入

[英]How to take input from ComboBox in PyQt4

I have created a combobox in PyQt4. 我在PyQt4中创建了一个组合框。 This combobox will have 5 options and the user needs to select one option among it and click the submit button. 该组合框将有5个选项,用户需要在其中选择一个选项,然后单击“提交”按钮。 I have tried to define a function called printing action to be used after the user clicks the submit button 我试图定义一个称为打印动作的功能,以便在用户单击“提交”按钮后使用

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)

Can you help me in understanding how to take the input after the user has selected one among the given options and presses the submit button 您能帮助我理解用户在给定选项中选择一个并按下“提交”按钮后如何进行输入

Here's an example where it will print the comboBox's current text and index: 这是一个示例,它将打印comboBox的当前文本和索引:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM