清除QTreeWidget时PyQt5 GUI崩溃

2024-07-03 06:50:54 发布

您现在位置:Python中文网/ 问答频道 /正文

当#从下面的代码行中删除时,为什么单击按钮时GUI崩溃?在

代码中的行是:

self.treeWidget.currentItemChanged.connect(self.current_selection)

from PyQt5 import QtWidgets

class Ui_Form(object):

    def setupUi(self, Form):

        Form.setWindowTitle("Test")
        Form.resize(600, 400)

        self.gridLayout = QtWidgets.QGridLayout(Form)

        self.treeWidget = QtWidgets.QTreeWidget(Form)
        self.treeWidget.setColumnCount(3)
        self.treeWidget.setHeaderLabels(['No.', 'Colour', 'Animal'])
        #self.treeWidget.currentItemChanged.connect(self.current_selection)

        self.buttonWidget1 = QtWidgets.QPushButton('Click1')
        self.buttonWidget1.clicked.connect(self.test1)

        self.gridLayout.addWidget(self.treeWidget, 0, 0)
        self.gridLayout.addWidget(self.buttonWidget1, 1, 0)

        self.populate()

    def test1(self):
        self.treeWidget.clear()

    def current_selection(self):
        item = self.treeWidget.currentItem()
        self.no = item.text(0)
        self.colour = item.text(1)
        self.animal = item.text(2)
        print(self.no, self.colour, self.animal)

    def populate(self):
        item = QtWidgets.QTreeWidgetItem(['1', 'Yellow', 'Dog'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['2', 'Black', 'Cat'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['3', 'Green', 'Frog'])
        self.treeWidget.addTopLevelItem(item)

        item = QtWidgets.QTreeWidgetItem(['4', 'Blue', 'Snail'])
        self.treeWidget.addTopLevelItem(item)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

如果有人能向我解释发生了什么事,我将不胜感激。在


Tags: textselfformdefconnectsyscurrentitem
2条回答

默认情况下,如果没有选择,currentItem()是树中的第一项。在

单击按钮时,self.treeWidget.clear()被调用。所有的项都从tree小部件中删除,因此不再有第一项:发出信号currentItemChanged。在

如果树中没有任何内容,self.treeWidget.currentItem()将返回None。在current_selectionitem是{},这就是为什么你得到:

AttributeError: 'NoneType' object has no attribute 'text'

要解决此问题,可以在继续操作之前测试item是否为{}:

def current_selection(self):
    item = self.treeWidget.currentItem()
    if item is not None:
        self.no = item.text(0)
        self.colour = item.text(1)
        self.animal = item.text(2)
    ...

要回答这个特定的问题:为什么GUI崩溃?在

这是由于PyQt-5.5引入了新的行为。引用PyQt5文档:

In PyQt v5.5 an unhandled Python exception will result in a call to Qt’s qFatal() function. By default this will call abort() and the application will terminate. Note that an application installed exception hook will still take precedence.

幸运的是,正如最后一句话所示,通过安装^{}可以绕过这种新行为,这将允许您的程序以更优雅的方式处理未处理的异常。在

作为最低限度的要求,简单地将回溯打印到stdout/stderr的旧行为可以恢复如下:

def except_hook(cls, exception, traceback):
    sys.__excepthook__(cls, exception, traceback)

if __name__ == "__main__":

    import sys
    sys.excepthook = except_hook

但显然,在某种消息框中显示错误将是一种更好的方式来通知用户发生了问题。在

相关问题 更多 >