访问索引框值

2024-09-27 00:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含2个项目的组合框-Method01, Method02 当选择了Method01时,我如何告诉我的代码执行Method01Func,对于Method02也是如此?在

self.connect(self.exportCombo, SIGNAL('currentIndexChanged(int)'), self.Method01Func)

在访问list-[0],[1]时,我试图用类似的代码来编写它。。。但我被错误所绊倒


Tags: 项目代码selfsignalconnect错误listint
2条回答

一种方法是在调用^{}时使用userData参数,并传递对您希望该项调用的函数的引用。在

下面是一个简单的例子:

import sys
from PyQt4 import QtCore, QtGui

def Method01Func():
    print 'method 1'

def Method02Func():
    print 'method 2'

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        widget = QtGui.QWidget()
        self.combo = QtGui.QComboBox()
        self.combo.addItem('Method 1', Method01Func)
        self.combo.addItem('Method 2', Method02Func)
        self.combo.currentIndexChanged.connect(self.execute_method)
        layout = QtGui.QVBoxLayout(widget)
        layout.addWidget(self.combo)
        self.setCentralWidget(widget)

@QtCore.pyqtSlot(int)
def execute_method(self, index):
    method = self.combo.itemData(index).toPyObject()
    method()

app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

或者,您可以发送当前项目文本,并发出以下信号:

self.exportCombo.currentIndexChanged[str].connect(self.execute_method)

然后在插槽中检查:

^{pr2}$

相关问题 更多 >

    热门问题