1

主窗口在 Class1 中声明。我正在尝试创建 Class2 的对象,创建一个小部件(一个按钮)并将其连接到一个插槽。

import sys
from PyQt4 import QtGui,QtCore

class Class2(object):
    def __init__(self,parent):
        return
    def button(self,parent):
        self.print_button=QtGui.QPushButton("print hello",parent)
        self.print_button.show()
        self.print_button.clicked.connect(self.print_hello)

    def print_hello(self,parent):
        print 'hello'



class Class1(QtGui.QMainWindow):
    def __init__(self):
        super(Class1, self).__init__()
        self.welcomeScreen()

    def welcomeScreen(self):
        obj=Class2(self)
        obj.button(self)


def main():
    app = QtGui.QApplication(sys.argv)
    mw = Class1()
    mw.show()
    sys.exit(app.exec_())


if __name__=='__main__':
    main()

现在,按钮正在创建,但插槽不起作用。如何处理这个问题?

4

1 回答 1

0

print_hello需要 2 个参数,而您只传递一个参数。

试试这个:

self.print_button.clicked.connect(lambda: self.print_hello(self))
于 2013-02-08T14:49:51.130 回答