【问题标题】:How to take input from ComboBox in PyQt4如何从 PyQt4 中的 ComboBox 获取输入
【发布时间】:2015-08-25 05:48:07
【问题描述】:

我在 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)

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

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2018-02-03
    • 2019-02-08
    • 2015-11-20
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多