AttributeError: tuple obj has no attribute in .py script with PySimpleGUI

如何解决AttributeError: tuple obj has no attribute in .py script with PySimpleGUI

我编写了一个 Python 脚本,该脚本在文件夹结构中循环并查看文件内部的给定值。该脚本运行良好,但现在我正在尝试添加 GUI 并且无法通过此错误。在gui中点击'search'时发生错误,触发事件。

文件“C:\Users\xxxxx\Documents\Python Scripts\FileContentsSearcherwWithFileWrite_GUIv02.py”,第 66 行,在 事件,值 = window().read AttributeError: 'tuple' 对象没有属性 'read'

认为我知道在脚本的某个地方有一个元组试图读取/显示窗口,但我不知道它可能是什么。有人可以帮忙吗?

谢谢

import os,PySimpleGUI as sg

document_ext = ['.SVG','.txt','.XML']

layout = [
           [
            sg.Text("This program can be used to search for a particular \nterm in all files under the folder location provided.") 
           ],[
            sg.ListBox(document_ext,size=(10,5),key="-File_Ext-")
           ],[
            sg.Text('What would you like to search for?')
           ],[
            sg.InputText(size=(30,key="-Search_Term-")
           ],[
            sg.Text("Choose Folder to Search:")
            ],[
            sg.In(size=(30,key="-FOLDER-"),sg.Folderbrowse()
           ],[
            sg.Text("Where Should Report Be Saved?")
            ],key="-FOLDER2-"),[
            sg.Button(button_text="Search")    
           ],[
            sg.Text(key="-Output-",size=(30,5))    
           ]
        ]

window = sg.Window("File Contents Searcher",layout)#,margins=(200,200))

def main(svalue,location,ext):
    number_found = 0
    search_results = ""
    os.chdir(location)
    for dpath,dname,fname in os.walk(os.getcwd()):
        for name in fname:
            pat = os.path.join(dpath,name)
            if name.endswith(ext):
                with open(pat) as f:
                    if svalue in f.read():
                        number_found += 1
                        search_results += "--- \nFilename: {} \nFilepath: {} \n".format(name,pat)
    search_results_head = "\"{}\" was found in {} files. \n \n".format(svalue,number_found)
    output = "RESULTS \n \n" + search_results_head + search_results
    return output,search_results_head

def create_log(sl,s_res):
     os.chdir(sl)
     print(os.getcwd())
     with open("FileSearchResults.txt","w") as f:
         f.write(s_res)
     return "Report Saved"

while True:
    event,values = window().read
    if event == sg.WIN_CLOSED:
        break
    if event == "Search":
        m = main("-Search_Term-",r"-FOLDER-","-File_Ext-")
        c = create_log(r"-FOLDER2-",m[0])
        window("-Output-").update(print(m[1] + " " + c))
window.close() 

解决方法

你可以试试这个。

import os,PySimpleGUI as sg

document_ext = ['.SVG','.txt','.XML']

layout = [
           [
            sg.Text("This program can be used to search for a particular \nterm in all files under the folder location provided.") 
           ],[
            sg.Listbox(document_ext,size=(10,5),key="-File_Ext-")
           ],[
            sg.Text('What would you like to search for?')
           ],[
            sg.InputText(size=(30,key="-Search_Term-")
           ],[
            sg.Text("Choose Folder to Search:")
            ],[
            sg.In(size=(30,key="-FOLDER-"),sg.FolderBrowse()
           ],[
            sg.Text("Where Should Report Be Saved?")
            ],key="-FOLDER2-"),[
            sg.Button(button_text="Search")    
           ],[
            sg.Multiline(key="-Output-",size=(30,5))    
           ]
        ]

window = sg.Window("File Contents Searcher",layout)#,margins=(200,200))

def main(svalue,location,ext):
    number_found = 0
    search_results = ""
    location = (values["-FOLDER-"]) # Set values to window.read() values
    svalue= (values["-Search_Term-"]) # Ditto for this
    ext = str(values["-File_Ext-"][0].lower()) # Needs this to choose value and make it case insensitive

    #os.chdir(location) # Don't need
    for dpath,dname,fname in os.walk(location): #Hardcoded to value above
        for name in fname:
            pat = os.path.join(dpath,name)
            if name.endswith(ext):
                with open(pat) as f:
                    if svalue in f.read():
                        number_found += 1
                        search_results += "--- \nFilename: {} \nFilepath: {} \n".format(name,pat)
    search_results_head = "\"{}\" was found in {} files. \n \n".format(svalue,number_found)
    output = "RESULTS \n \n" + search_results_head + search_results
    return output,search_results_head

def create_log(sl,s_res):
     s1 = (values["-FOLDER2-"])  # Hardcoded again
     print(os.getcwd())
     with open("FileSearchResults.txt","w") as f:
         f.write(s_res)
     return "Report Saved"

while True:
    event,values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == "Search":
        m = main("-Search_Term-","-FOLDER-","-File_Ext-")
        print(m)
        c = create_log(r"-FOLDER2-",m[0])
        window["-Output-"].update(m[1] + " " + c)
window.close() 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?