Inyoung Kim 김인영
Inyoung Kim 김인영

Reputation: 1597

clicked.connect() Error

I'm working on Window 10,PyCharm-Python 3.5.2

What I was trying to do: If PB1(push button 1) clicked, open a new window.

Problem: I get an error

 self.PB1.clicked.connect(self.Soft_Memory())
TypeError: argument 1 has unexpected type 'NoneType' 

Since I defined Soft_Memory(), I don't see why Soft_Memory() is NoneType. Though on the editor '.connect' gets highlighted and says cannot find reference 'connect' in 'function'

Codes are below. I've erased some part of the code so that its better to see. If anyone need the full code please comment.

SM.py

class SM_Window(QMainWindow, QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.initU()

    def initU(self):
        self.setWindowTitle("SM_Window")
        self.setGeometry(10, 30, 850, 850)

UI.py

import SM

class MainWindow(QMainWindow, QWidget):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.initUI()

    def Soft_Memory(self):
        self.SF = SM.SM_Window()
        self.SF.show()

    def Buttons(self):
        #Button for SF
        self.PB1 = QPushButton("POP", self)
        self.PB1.setToolTip("POPOPOPOPOPOP")
        self.PB1.move(100, 100)

    def Signal_to_Slot(self):
        self.PB1.clicked.connect(self.Soft_Memory())

    def initUI(self):
        self.setWindowTitle("UI")
        self.setGeometry(850, 850, 850, 850)
        self.Buttons()
        self.Signal_to_Slot()
        self.showMaximized()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec())

Upvotes: 2

Views: 7634

Answers (3)

Saelyth
Saelyth

Reputation: 1734

The answers from @DaveyH-cks and @user3419537 are correct, you should use a reference of the method, instead of calling it:

self.PB1.clicked.connect(self.Soft_Memory)

However, you might often need to pass arguments on those functions (I certainly do). On those situations, if you need to use args there's a workaround by using lambda.

self.PB1.clicked.connect(lambda: myfunction(self, arg1, True, "example", arg4))

Upvotes: 2

DaveyH-cks
DaveyH-cks

Reputation: 39

When you write self.Soft_Memory() you are calling the method by using the parenthesis. What you want is to reference the method:

self.PB1.clicked.connect(self.Soft_Memory)

Upvotes: 0

user3419537
user3419537

Reputation: 5000

The connect() method expects a callable argument. When you write self.Soft_Memory() you are making a call to that method, and the result of that call (None, since you don't explicitly return anything) is what is being passed to connect().

You want to pass a reference to the method itself.

self.PB1.clicked.connect(self.Soft_Memory)

Upvotes: 2

Related Questions