[Python] 纯文本查看 复制代码
# -*- coding:utf-8 -*-
import wx
import gzip
import os
import threading
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='解压工具', size=(410, 201), name='frame', style=wx.DEFAULT_FRAME_STYLE)
self.qdck = wx.Panel(self)
self.Centre()
self.jdt1 = wx.Gauge(self.qdck, range=100, size=(353, 24), pos=(16, 118), name='gauge', style=wx.GA_HORIZONTAL)
self.jdt1.SetValue(0)
self.bq1 = wx.StaticText(self.qdck, size=(360, 24), pos=(20, 17), label='将*.tsv.gz文件拖入至软件内即可解压。', name='staticText', style=wx.ST_ELLIPSIZE_END)
self.bq2 = wx.StaticText(self.qdck, size=(360, 24), pos=(21, 47), label='等待文件...', name='staticText', style=wx.ST_ELLIPSIZE_END)
self.SetDropTarget(FileDrop(self))
self.Show()
class FileDrop(wx.FileDropTarget):
def __init__(self, frame):
wx.FileDropTarget.__init__(self)
self.frame = frame
def OnDropFiles(self, x, y, filePaths):
if filePaths:
path = filePaths[0]
if path.lower().endswith('.tsv.gz'):
self.frame.bq2.SetLabel("正在解压...")
self.frame.jdt1.SetValue(0)
thread = DecompressThread(path, self.frame)
thread.start()
else:
wx.MessageBox("请选择有效的 .tsv.gz 文件!", "错误", wx.OK | wx.ICON_ERROR)
return True
class DecompressThread(threading.Thread):
def __init__(self, file_path, frame):
super().__init__()
self.file_path = file_path
self.frame = frame
def run(self):
try:
output_path = self.file_path.replace(".gz", "")
with gzip.open(self.file_path, 'rb') as gz_file:
with open(output_path, 'wb') as out_file:
total_size = os.path.getsize(self.file_path)
bytes_read = 0
chunk_size = 1024 * 1024 # 每次读取1MB
while True:
chunk = gz_file.read(chunk_size)
if not chunk:
break
out_file.write(chunk)
bytes_read += len(chunk)
progress = int((bytes_read / total_size) * 100)
wx.CallAfter(self.frame.jdt1.SetValue, min(progress, 100))
wx.CallAfter(self.frame.bq2.SetLabel, "解压完成!")
# wx.CallAfter(self.show_tsv_data, output_path)
print("ok")
except Exception as e:
wx.CallAfter(wx.MessageBox, f"解压失败: {str(e)}", "错误", wx.OK | wx.ICON_ERROR)
wx.CallAfter(self.frame.bq2.SetLabel, "解压失败,请重试。")
def show_tsv_data(self, tsv_path):
print(tsv_path)
class MyApplication(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
return True
if __name__ == '__main__':
app = MyApplication()
app.MainLoop()