简体   繁体   English

PySide2:AttributeError:'NoneType' object 没有属性'objectName'

[英]PySide2 : AttributeError: 'NoneType' object has no attribute 'objectName'

How can I fix this error:我该如何解决这个错误:

Traceback (most recent call last):回溯(最近一次通话最后):

File "", line 32, in run_setup_turn_table文件“”,第 32 行,在 run_setup_turn_table 中

AttributeError: 'NoneType' object has no attribute 'objectName' AttributeError: 'NoneType' object 没有属性 'objectName'

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import sys

class mk_preview_UI(QWidget):
    def __init__(self):
        super(mk_preview_UI,self).__init__()
        
        self.UI()
        self.layout()
        self.show()
    def UI(self):
        self.setup_turnTable = QPushButton("Setup turn table")
        self.setup_turnTable.setObjectName("run_setup_btn")
        
    def layout(self):
        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.setup_turnTable)
        self.setLayout(self.main_layout)



class run_ui:
    def __init__(self):
        self.MainWindow = mk_preview_UI()
        self.MainWindow.setup_turnTable.clicked.connect(self.run_setup_turn_table)

    def run_setup_turn_table(self):
        run_button = self.MainWindow.sender()
        if run_button.objectName() == "run_setup_btn":
           print "run_setup_btn"       
        


if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = run_ui()
    sys.exit(app.exec_())

Note: Seems a PySide2 bug.注意:似乎是 PySide2 错误。

If a QObject emits the signal and invokes the method of another QObject then sender() of the receiver object will return the emitter object.如果一个 QObject 发出信号并调用另一个 QObject 的方法,那么接收器 object 的 sender() 将返回发射器 object。 In your case MainWindow is not the receiver so the sender() will not return the object that emits the signal.在您的情况下, MainWindow 不是接收器,因此 sender() 不会返回发出信号的 object 。 A workaround is to make run_ui a QObject and since the run_setup_turn_table method belongs to it, then the sender() will return the button.一种解决方法是使 run_ui 成为 QObject,并且由于 run_setup_turn_table 方法属于它,因此 sender() 将返回按钮。

class run_ui(QObject):
    def __init__(self):
        super().__init__()
        self.MainWindow = mk_preview_UI()
        self.MainWindow.setup_turnTable.clicked.connect(self.run_setup_turn_table)

    def run_setup_turn_table(self):
        run_button = self.sender()
        if run_button.objectName() == "run_setup_btn":
            print("run_setup_btn")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PySide2:QOpenGLContext'对象没有属性'functions' - PySide2: QOpenGLContext' object has no attribute 'functions' PySide2 自定义信号错误:“AttributeError: 'function' object 没有属性 'connect'” - PySide2 Custom Signal Error: “AttributeError: 'function' object has no attribute 'connect'” AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' Maya中的PySide2-“模块”对象没有属性“ QPushButton” - PySide2 in Maya - 'module' object has no attribute 'QPushButton' AttributeError : 'NoneType' 对象没有属性 '_meta' - AttributeError : 'NoneType' object has no attribute '_meta' AttributeError:“ NoneType”对象没有属性“ dtype” - AttributeError: 'NoneType' object has no attribute 'dtype' AttributeError:“ NoneType”对象没有属性“ encode” - AttributeError: 'NoneType' object has no attribute 'encode' attributeError: 'NoneType' 对象没有属性 'end' - attributeError: 'NoneType' object has no attribute 'end' AttributeError: 'NoneType' 对象没有属性 'index' - AttributeError: 'NoneType' object has no attribute 'index'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM