【问题标题】:how to get the all items exist in QlistWidget in PyQt5如何在 PyQt5 中获取 QlistWidget 中存在的所有项目
【发布时间】:2018-09-13 17:08:08
【问题描述】:

我有一个显示所选目录中存在的文件列表的功能,然后用户输入搜索的单词,程序在后台读取这些文件以找到匹配的单词,最后它覆盖现有列表只显示包含匹配单词的文件。

问题在于 while 循环 系统显示此错误:

而索引

builtins.TypeError: 'int' 类型的对象没有 len()

代码:

def listFiles(self):

        readedFileList = []
        index = 0
        while index < len(self.listWidgetPDFlist.count()):
            readedFileList.append(self.listWidgetPDFlist.item(index))
        print(readedFileList)

        try:
            for file in readedFileList:

                with open(file) as lstf:
                    filesReaded = lstf.read()
                    print(filesReaded)
                return(filesReaded)

        except Exception as e:
            print("the selected file is not readble because :  {0}".format(e))     

【问题讨论】:

    标签: python pyqt pyqt5 qlistwidget qlistwidgetitem


    【解决方案1】:

    count() 返回项目数,所以它是一个整数,函数 len() 仅适用于可迭代,而不适用于整数,所以你会得到那个错误,而且它不是必需的。您必须执行以下操作:

    def listFiles(self):
        readedFileList = [self.listWidgetPDFlist.item(i).text() for i in range(self.listWidgetPDFlist.count())]
        try:
            for file in readedFileList:
                with open(file) as lstf:
                    filesReaded = lstf.read()
                    print(filesReaded)
                    # return(filesReaded)
    
        except Exception as e:
            print("the selected file is not readble because :  {0}".format(e)) 
    

    注意:不要使用return,你将在第一次迭代中完成循环。

    【讨论】:

    • 我试过你的答案,所以现在该函数读取 QlistWidget 中的所有现有文件...谢谢...我想要的是用户输入搜索的单词后系统覆盖现有列表并仅显示包含匹配表达式的文件....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2018-08-10
    • 2014-04-29
    • 1970-01-01
    相关资源
    最近更新 更多