import wx
import os
import pandas as pd
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, title=title, size=(400, 200))
panel = wx.Panel(self)
self.output_dir_btn = wx.Button(panel, label="选择输出文件夹")
self.Bind(wx.EVT_BUTTON, self.on_select_output_dir, self.output_dir_btn)
self.input_file_btn = wx.Button(panel, label="选择Excel文件")
self.Bind(wx.EVT_BUTTON, self.on_select_input_file, self.input_file_btn)
self.start_btn = wx.Button(panel, label="开始生成")
self.Bind(wx.EVT_BUTTON, self.on_start_generation, self.start_btn)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.output_dir_btn, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.input_file_btn, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.start_btn, 0, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
def on_select_output_dir(self, event):
dlg = wx.DirDialog(self, "选择输出文件夹", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.output_dir = dlg.GetPath()
print("输出文件夹:", self.output_dir)
dlg.Destroy()
def on_select_input_file(self, event):
dlg = wx.FileDialog(self, "选择Excel文件", wildcard="Excel files (*.xlsx)|*.xlsx",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.input_file = dlg.GetPath()
print("输入文件:", self.input_file)
dlg.Destroy()
def on_start_generation(self, event):
if not hasattr(self, 'output_dir') or not hasattr(self, 'input_file'):
wx.MessageBox("请先选择输出文件夹和Excel文件", "错误", wx.OK | wx.ICON_ERROR)
return
df = pd.read_excel(self.input_file)
for i, col in enumerate(df.iloc[:, 0]):
column_names = col.split(',')
file_name = f"{i+1}.xlsx"
file_path = os.path.join(self.output_dir, file_name)
df_new = pd.DataFrame(columns=column_names)
df_new.to_excel(file_path, index=False)
wx.MessageBox("生成完成", "提示", wx.OK | wx.ICON_INFORMATION)
self.Close()
app = wx.App()
frame = MyFrame(None, "Excel文件生成器")
frame.Show()
app.MainLoop()```
在上述代码中,我们创建了一个`MyFrame`类,继承自wxPython的`Frame`类。该类表示我们的应用程序窗口,并包含了选择输出文件夹和选择Excel文件的按钮。
**3. 实现回调函数**
接下来,我们需要实现与按钮关联的回调函数。这些函数将在用户点击相应的按钮时被调用。
```python
def on_select_output_dir(self, event):
dlg = wx.DirDialog(self, "选择输出文件夹", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.output_dir = dlg.GetPath()
print("输出文件夹:", self.output_dir)
dlg.Destroy()
def on_select_input_file(self, event):
dlg = wx.FileDialog(self, "选择Excel文件", wildcard="Excel files (*.xlsx)|*.xlsx",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dlg.ShowModal() == wx.ID_OK:
self.input_file = dlg.GetPath()
print("输入文件:", self.input_file)
dlg.Destroy()
def on_start_generation(self, event):
if not hasattr(self, 'output_dir') or not hasattr(self, 'input_file'):
wx.MessageBox("请先选择输出文件夹和Excel文件", "错误", wx.OK | wx.ICON_ERROR)
return
df = pd.read_excel(self.input_file)
for i, col in enumerate(df.iloc[:, 0]):
column_names = col.split(',')
file_name = f"{i+1}.xlsx"
file_path = os.path.join(self.output_dir, file_name)
df_new = pd.DataFrame(columns=column_names)
df_new.to_excel(file_path, index=False)
wx.MessageBox("生成完成", "提示", wx.OK | wx.ICON_INFORMATION)
self.Close()
|