PyQt4--QPushButton阵列

http://stackoverflow.com/questions/5364291/how-to-pass-arguments-to-a-function-using-a-predefined-signal-in-pyqt

# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import functools

class Dialog(QDialog):
    def __init__(self,parent=None):
        super(Dialog,self).__init__(parent)
        self.setButtons(row=5,column=5)
        for row_index in range(len(self.btns)):
            for column_index in range(len(self.btns[row_index])):
                self.connect(self.btns[row_index][column_index],
                    SIGNAL('clicked()'),
                    functools.partial(self.btnClicked,row_index,column_index))
    def setButtons(self,row=2,column=2):
        self.btns=list()
        for row_index in range(row):
            row_btn=list()
            for column_index in range(column):
                btn=QPushButton('Row %d Column %d'%(row_index,column_index))
                row_btn.append(btn)
            self.btns.append(row_btn)
        gridLayout=QGridLayout(self)
        for row_index in range(row):
            for column_index in range(column):
                gridLayout.addWidget(self.btns[row_index][column_index],row_index,column_index)
    def btnClicked(self,row_index,column_index):
        QMessageBox.information(self,'Msg','Row %d, Column %d Button Clicked'%(row_index,column_index))
app=QApplication(sys.argv)
dlg=Dialog()
dlg.show()
sys.exit(app.exec())

PyQt4--QPushButton阵列_第1张图片

你可能感兴趣的:(list,Class,dialog,import,button,Signal)