最近在做一个文件管理的小工具,需要列出选择的文件夹下的所有zip压缩文件,并在点击某个zip文件时能够显示其中的内容。为此我使用了wxpython来实现这个功能。
首先导入程序需要的模块:
import wx
import os
import zipfile
使用wx.Frame来创建一个主窗口,里面放一个Panel,然后在Panel上添加2个wx.ListBox作为zip文件列表和文件列表,以及一个wx.Button用于选择文件夹:
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Zip File Viewer")
self.panel = wx.Panel(self)
self.zip_list = wx.ListBox(self.panel)
self.file_list = wx.ListBox(self.panel)
self.select_btn = wx.Button(self.panel, label="Select Folder")
使用wx.BoxSizer来设置主窗口的布局,列表框水平排列,选择按钮在上方:
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.zip_list, 1, wx.EXPAND)
sizer.Add(self.file_list, 1, wx.EXPAND)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.select_btn, 0, wx.ALL|wx.CENTER, 5)
main_sizer.Add(sizer, 1, wx.ALL|wx.EXPAND, 5)
self.panel.SetSizer(main_sizer)
下面需要绑定按钮和列表框的点击事件,以执行对应的功能:
self.select_btn.Bind(wx.EVT_BUTTON, self.onSelectFolder)
self.zip_list.Bind(wx.EVT_LISTBOX, self.onZipFileSelected)
选择文件夹按钮的事件处理函数中使用wx.DirDialog来选择文件夹,并保存路径。然后调用方法来列出该路径下的zip文件:
def onSelectFolder(self, event):
dialog = wx.DirDialog(self, "Choose folder", style=wx.DD_DEFAULT_STYLE)
if dialog.ShowModal() == wx.ID_OK:
self.folder_path = dialog.GetPath()
self.updateZipFileList()
dialog.Destroy()
def updateZipFileList(self):
self.zip_list.Clear()
if self.folder_path:
files = os.listdir(self.folder_path)
for f in files:
if f.endswith('.zip'):
self.zip_list.Append(f)
点击zip文件时,会获取选中的文件名,打开zip文件,并列出其中的所有文件名:
def onZipFileSelected(self, event):
zip_file = self.zip_list.GetStringSelection()
full_path = os.path.join(self.folder_path, zip_file)
self.file_list.Clear()
with zipfile.ZipFile(full_path) as zf:
for info in zf.infolist():
self.file_list.Append(info.filename)
最后简单创建wx.App和MyFrame对象,启动主循环:
app = wx.App()
frame = MyFrame()
app.MainLoop()
import wx
import os
import zipfile
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Zip File Viewer")
self.panel = wx.Panel(self)
self.folder_path = ""
self.zip_list = wx.ListBox(self.panel)
self.file_list = wx.ListBox(self.panel)
self.select_btn = wx.Button(self.panel, label="Select Folder")
self.select_btn.Bind(wx.EVT_BUTTON, self.onSelectFolder)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.zip_list, 1, wx.EXPAND)
sizer.Add(self.file_list, 1, wx.EXPAND)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.select_btn, 0, wx.ALL|wx.CENTER, 5)
main_sizer.Add(sizer, 1, wx.ALL|wx.EXPAND, 5)
self.panel.SetSizer(main_sizer)
self.Show()
self.zip_list.Bind(wx.EVT_LISTBOX, self.onZipFileSelected)
def onSelectFolder(self, event):
dialog = wx.DirDialog(self, "Choose folder", style=wx.DD_DEFAULT_STYLE)
if dialog.ShowModal() == wx.ID_OK:
self.folder_path = dialog.GetPath()
self.updateZipFileList()
dialog.Destroy()
def updateZipFileList(self):
self.zip_list.Clear()
if self.folder_path:
files = os.listdir(self.folder_path)
for f in files:
if f.endswith('.zip'):
self.zip_list.Append(f)
def onZipFileSelected(self, event):
zip_file = self.zip_list.GetStringSelection()
full_path = os.path.join(self.folder_path, zip_file)
self.file_list.Clear()
with zipfile.ZipFile(full_path) as zf:
for info in zf.infolist():
self.file_list.Append(info.filename)
app = wx.App()
frame = MyFrame()
app.MainLoop()
这就实现了一个简单的文件管理器,可以选择文件夹,显示zip文件,并查看zip内容。wxpython提供了丰富的界面组件和事件绑定机制,使用起来非常方便和高效。