Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t使用Pyqt5选中QlistWidget中的复选框,系统将在python中显示错误_Python_Checkbox_Qlistwidget - Fatal编程技术网

can';t使用Pyqt5选中QlistWidget中的复选框,系统将在python中显示错误

can';t使用Pyqt5选中QlistWidget中的复选框,系统将在python中显示错误,python,checkbox,qlistwidget,Python,Checkbox,Qlistwidget,我有一个在QlistWidget中显示项目的代码,在每个项目旁边都有一个复选框,用户可以为将来的任务签入该复选框 问题在于,当用户在单击项目文本之前选中复选框时 系统崩溃并显示以下错误: 第126行,在选定的文件列表中 Item=self.listWidgetPDFlist.currentItem().text() builtins.AttributeError:“非类型”对象没有属性“文本” 代码: 我的函数中有什么错误?self.listWidgetPDFlist.currentItem()

我有一个在QlistWidget中显示项目的代码,在每个项目旁边都有一个复选框,用户可以为将来的任务签入该复选框

问题在于,当用户在单击项目文本之前选中复选框时

系统崩溃并显示以下错误:

第126行,在选定的文件列表中
Item=self.listWidgetPDFlist.currentItem().text()

builtins.AttributeError:“非类型”对象没有属性“文本”

代码:
我的函数中有什么错误?

self.listWidgetPDFlist.currentItem()
在当前未选择任何项时返回None

通常,它会返回一个列表小部件项,该项具有
.text()
方法,但没有,因此它会失败

您可以这样修复错误:

def FileListSelected(self):
    print(self.listWidgetPDFlist.currentItem())
    Item=self.listWidgetPDFlist.currentItem()
    return Item.text() if Item else None

但是,向QCheckBox添加一个返回其父QListWidgetItem的方法可能更好

如何向QCheckBox写入一个返回其父QListWidgetItem的方法?
def FileListSelected(self):
    print(self.listWidgetPDFlist.currentItem())
    Item=self.listWidgetPDFlist.currentItem()
    return Item.text() if Item else None