1

我试图剥离我的代码,直到几乎什么都没有,但似乎仍然无法将我的addButton小部件连接到我的self.add方法。我确定我错过了一些非常明显的东西,但我的眼睛没有捕捉到它..

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class StringListDlg(QDialog):

    def __init__(self, name, strings, parent=None):
        super(StringListDlg, self).__init__(parent)

        self.listWidget = QListWidget()
        self.listWidget.addItems(strings)
        addButton = QPushButton("&Add...")

        grid = QGridLayout()
        grid.addWidget(self.listWidget, 0, 0, 7, 1)

        self.setLayout(grid)
        self.setWindowTitle("Edit {} List".format(name))

        self.connect(addButton, SIGNAL("clicked()"), self.add)

        def add(self):
            pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = StringListDlg("Fruit", ['apples', 'bananas'])
    form.exec_()

这是我的回溯:

Traceback (most recent call last):
  File "C:/pyqt/chap05/test.py", line 27, in <module>
    form = StringListDlg("Fruit", ['apples', 'bananas'])
  File "C:/pyqt/chap05/test.py", line 20, in __init__
    self.connect(addButton, SIGNAL("clicked()"), self.add)
AttributeError: 'StringListDlg' object has no attribute 'add'
4

0 回答 0