gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:32:26 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com