PyQt4 PushButton连接问题

时间:2015-12-11 11:39:14

标签: python-2.7 pyqt4

有一个类GUI_XMLtool,它是从.ui QtDesigner文件生成的。 还有一个MyApp课程。现在我尝试将PushButton(XSD_path_PB)点击连接到MyApp方法调用。我通过两种方式尝试(其中一种被评论)

from PyQt4 import QtCore, QtGui
from GUI import GUI_XMLtool
import sys

class MyApp(QtGui.QMainWindow, GUI_XMLtool):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = GUI_XMLtool()
        self.ui.setupUi(self)

        self.connect(self.ui.XSD_path_PB, QtCore.SIGNAL("clicked()"), self.someMethod())
        #self.ui.XSD_path_PB.clicked.connect(self.someMethod())

    def someMethod(self):
        print "wahwah"

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

这是最简单的事情,但我得到了一个可怕的错误跟踪:

wahwah
Traceback (most recent call last):
  File "C:/Users/***/PycharmProjects/XMLTool/GUI/Main.py", line 20, in <module>
    window = MyApp()
  File "C:/Users/***/PycharmProjects/XMLTool/GUI/Main.py", line 12, in __init__
    self.connect(self.ui.XSD_path_PB, QtCore.SIGNAL("clicked()"), self.someMethod())
TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'

有什么问题?

1 个答案:

答案 0 :(得分:0)

使用新的样式信号和插槽(评论的那个),你应该写:

self.ui.XSD_path_PB.clicked.connect(self.someMethod)

而不是

self.ui.XSD_path_PB.clicked.connect(self.someMethod())

执行第二行时,调用self.someMethod(),并返回一个值(在您的情况下为默认返回值None)。然后,使用此值调用connect。它期望这个值是一个python可调用的(一个方法),但它不是(它是None),所以你得到一个TypeError

要使python可调用,只需使用self.someMethod

即可
>>>type(someMethod)                                                                                                                 
<class 'function'>