[英]How to take input from ComboBox in 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)
您能幫助我理解用戶在給定選項中選擇一個並按下“提交”按鈕后如何進行輸入
這是一個示例,它將打印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.