python - AttributeError:元组 obj 在 PySimpleGUI 的 .py 脚本中没有属性
问题描述
我编写了一个 Python 脚本,该脚本循环遍历文件夹结构并在文件中查找给定值。该脚本运行良好,但现在我正在尝试添加 GUI,但无法通过此错误。单击 gui 中的“搜索”时会发生错误,触发事件。
文件“C:\Users\xxxxx\Documents\Python Scripts\FileContentsSearcherwWithFileWrite_GUIv02.py”,第 66 行,在事件中,values = window().read AttributeError: 'tuple' object has no attribute '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,5), key="-Search_Term-")
],
[
sg.Text("Choose Folder to Search:")
],
[
sg.In(size=(30,5), key="-FOLDER-"),
sg.FolderBrowse()
],
[
sg.Text("Where Should Report Be Saved?")
],
[
sg.In(size=(30,5), key="-FOLDER2-"),
sg.FolderBrowse()
],
[
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,5), key="-Search_Term-")
],
[
sg.Text("Choose Folder to Search:")
],
[
sg.In(size=(30,5), key="-FOLDER-"),
sg.FolderBrowse()
],
[
sg.Text("Where Should Report Be Saved?")
],
[
sg.In(size=(30,5), key="-FOLDER2-"),
sg.FolderBrowse()
],
[
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()
推荐阅读
- sql - BigQuery AVG 按小时计算
- modx - 如何跳过插件代码的执行?
- r - 编译R markdown时过滤错误
- html - 为什么没有 ID 或祖先类就不能应用我的 CSS 规则?
- java - Cache2K auto refreshAhead 没有按预期工作
- ios - Amplify 无法从 s3 下载对象
- php - $_GET 返回格式不正确的数值,但标准变量不返回?
- javascript - form.submit() 和 form.reset() 出现问题
- c - 当 c 程序运行时,打印大量的垃圾
- ios - 使用 Image 和 TitleText 自定义 UINavigationBar