Python+wxPython实现文件内容搜索工具
作者:winfredzhang
前言
在本篇文章中,我们将介绍如何使用 wxPython 库创建一个简单的文件搜索工具。这个工具允许用户选择一个文件夹,并在该文件夹中的所有 .py 文件中查找指定的文字,并显示匹配的位置。C:\pythoncode\blog\searchwordinpyfile.py
效果如下
代码实现
我们首先导入必要的模块:
import os import wx
接下来,我们定义一个名为 SearchFrame
的类,这个类继承自 wx.Frame
,用于创建搜索工具的主窗口。
class SearchFrame(wx.Frame): def __init__(self, parent, title): super(SearchFrame, self).__init__(parent, title=title, size=(400, 400)) # 创建界面元素 self.panel = wx.Panel(self) # ... 省略其他界面元素的创建和布局代码 ... # 绑定按钮点击事件 self.folder_button.Bind(wx.EVT_BUTTON, self.on_select_folder) self.search_button.Bind(wx.EVT_BUTTON, self.on_search) # ... 省略其他事件处理函数的实现 ... # 创建应用程序对象 app = wx.App() frame = SearchFrame(None, title="文件搜索") frame.Show() # 运行应用程序主循环 app.MainLoop()
以上代码中,我们创建了一个 SearchFrame
类,并在其构造函数中初始化界面元素,并绑定了按钮的点击事件。接下来,我们定义了两个事件处理函数 on_select_folder
和 on_search
,分别用于处理选择文件夹按钮和搜索按钮的点击事件。
在 on_select_folder
函数中,我们使用 wx.DirDialog
创建了一个选择文件夹的对话框,并获取用户选择的文件夹路径。
在 on_search
函数中,我们首先获取用户选择的文件夹路径和搜索文字,然后遍历指定文件夹下的所有 .py 文件,打开每个文件并读取内容,查找是否包含搜索文字,如果存在匹配,则记录匹配的位置。
最后,我们创建了一个 wxPython 的 App 对象,并创建了一个 SearchFrame
实例,并显示在界面上。通过调用 app.MainLoop()
,我们使程序进入事件处理循环,等待用户的操作。
全部代码
import os import wx class SearchFrame(wx.Frame): def __init__(self, parent, title): super(SearchFrame, self).__init__(parent, title=title, size=(400, 400)) # 创建界面元素 self.panel = wx.Panel(self) self.folder_button = wx.Button(self.panel, label="选择文件夹") self.search_label = wx.StaticText(self.panel, label="搜索文字:") self.search_text = wx.TextCtrl(self.panel) self.search_button = wx.Button(self.panel, label="搜索") self.memo = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE) # 设置界面布局 sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.folder_button, 0, wx.ALL, 5) sizer.Add(self.search_label, 0, wx.ALL, 5) sizer.Add(self.search_text, 0, wx.EXPAND|wx.ALL, 5) sizer.Add(self.search_button, 0, wx.ALL, 5) sizer.Add(self.memo, 1, wx.EXPAND|wx.ALL, 5) self.panel.SetSizer(sizer) # 绑定按钮点击事件 self.folder_button.Bind(wx.EVT_BUTTON, self.on_select_folder) self.search_button.Bind(wx.EVT_BUTTON, self.on_search) def on_select_folder(self, event): dialog = wx.DirDialog(self, "选择文件夹", style=wx.DD_DEFAULT_STYLE) if dialog.ShowModal() == wx.ID_OK: folder_path = dialog.GetPath() self.folder_button.SetLabel(folder_path) dialog.Destroy() def on_search(self, event): folder_path = self.folder_button.GetLabel() search_text = self.search_text.GetValue() matches = [] # 遍历指定文件夹下的所有 .py 文件 for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith(".py"): file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8") as f: content = f.read() if search_text in content: match_positions = [pos for pos in range(len(content)) if content.startswith(search_text, pos)] matches.append((file_path, match_positions)) # 在 Memo 组件中显示找到的文件名和文字位置 # self.memo.Clear() for match in matches: file_path, positions = match self.memo.AppendText(f"文件名: {file_path}\n") for position in positions: self.memo.AppendText(f"文字位置: {position}\n") self.memo.AppendText("\n") # 创建应用程序对象 app = wx.App() frame = SearchFrame(None, title="文件搜索") frame.Show() # 运行应用程序主循环 app.MainLoop()
总结
通过使用 wxPython 库,我们创建了一个简单的文件搜索工具,实现了选择文件夹、输入搜索文字并点击搜索按钮的功能。在指定的文件夹中,我们遍历了所有的 .py 文件,并查找包含搜索文字的位置,将结果显示在界面上。
到此这篇关于Python+wxPython实现文件内容搜索工具的文章就介绍到这了,更多相关Python wxPython文件内容搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!