質問

The main window is declared in Class1. I am trying to create an object of Class2, create a widget( a push button) and connect it to a slot.

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()

Now, the button is getting created but the slot is not working. How to deal with this problem?

役に立ちましたか?

解決

Your print_hello takes 2 arguments, and you pass it only a one.

Try this:

self.print_button.clicked.connect(lambda: self.print_hello(self))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top