Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

python - 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())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...