clicked.connect() 错误 [英] clicked.connect() Error

查看:246
本文介绍了clicked.connect() 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Window 10,PyCharm-Python 3.5.2

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

我想做什么:如果 PB1(按钮 1)被点击,打开一个新窗口.

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

问题:我收到一个错误

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

自从我定义了 Soft_Memory(),我不明白为什么 Soft_Memory() 是 NoneType.尽管在编辑器中.connect"被突出显示并说在function"中找不到引用connect"

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

推荐答案

connect() 方法需要一个可调用的参数.当您编写 self.Soft_Memory() 时,您正在调用该方法,并且该调用的结果(None,因为您没有明确返回任何内容)是传递给 connect() 的内容.

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)

这篇关于clicked.connect() 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆