sudeepdino008
sudeepdino008

Reputation: 3364

creation and usage of a user defined slot in PyQt

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__();
        self.initUI()


    def initUI(self):
        field = QtGui.QLineEdit("", self)
        field.resize(field.sizeHint())
        field.move(150, 100)

        submit_button = QtGui.QPushButton("Fill hello world", self)
        submit_button.resize(submit_button.sizeHint())
        submit_button.move(50,300)
        submit_button.clicked.connect(self.modify(field))


    def modify(self, field):
        field.setText("hello")

def main():    #!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__();
        self.initUI()


    def initUI(self):
        field = QtGui.QLineEdit("", self)
        field.resize(field.sizeHint())
        field.move(150, 100)

        submit_button = QtGui.QPushButton("Fill hello world", self)
        submit_button.resize(submit_button.sizeHint())
        submit_button.move(50,300)
        submit_button.clicked.connect(self.modify(field))


    def modify(self, field):
        field.setText("hello")


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()

Ok. so here is what I want to do.Whenever the sumbit button is clicked, I want the field to be filled by 'hello world'. This would mean connecting the submit button to a user defined slot. How do I pass the field to the modify() function, where its text can be altered?

Presently the code gives error:

     File "test.py", line 21, in initUI
    submit_button.clicked.connect(self.modify(field))
TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'

Upvotes: 1

Views: 192

Answers (1)

Mickaël Bucas
Mickaël Bucas

Reputation: 691

First you get the error message from connect() because you are not passing a function as the argument, but the result of the call of the function, which is None. This line should be written :

submit_button.clicked.connect(self.modify)

Then the modify function should be changed to know which field to fill. One way to achieve this is by keeping the field in a class variable, by prefixing it by self. :

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__();
        self.initUI()


    def initUI(self):
        self.field = QtGui.QLineEdit("", self)
        self.field.resize(self.field.sizeHint())
        self.field.move(150, 100)

        submit_button = QtGui.QPushButton("Fill hello world", self)
        submit_button.resize(submit_button.sizeHint())
        submit_button.move(50,300)
        submit_button.clicked.connect(self.modify)


    def modify(self):
        self.field.setText("hello")


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()

Upvotes: 1

Related Questions