import wx
import wx.xrc
import pandas as pd
from conf.env import *
# 允许选择的文件
wildcard = "Python source (*.xls; *.xlsx)|*.xls;*.xlsx"
class WxForms(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, id=wx.ID_ANY,
                          title="File conversion",
                          pos=wx.DefaultPosition,
                          size=(520, 200))
        # 框架布局
        panel = wx.Panel(self, wx.ID_ANY)
        # 输入文本域
        LabelCfn = wx.StaticText(panel, id=wx.ID_ANY, label='文件设置:', pos=(15, 5), size=(80, 25))
        self.Txt_Input = wx.TextCtrl(panel, id=wx.ID_ANY, pos=(95, 5), size=(300, 25))
        # 事件按钮
        btnO = wx.Button(panel, label="...", pos=(405, 5), size=(70, 25))
        # 生成文本路径
        labelCfn2 = wx.StaticText(panel, id=wx.ID_ANY, label='输出文件夹:', pos=(15, 45), size=(80, 25))
        self.Txt_Output = wx.TextCtrl(panel, id=wx.ID_ANY, pos=(95, 45), size=(300, 25))
        # 事件按钮
        btnS = wx.Button(panel, label="...", pos=(405, 45), size=(70, 25))
        # 事件执行按钮
        btnC = wx.Button(panel, label='执行', pos=(395, 85), size=(80, 35))
        # 事件绑定
        btnO.Bind(wx.EVT_BUTTON, self.onChioceFile)
       btnS.Bind(wx.EVT_BUTTON, self.OnChioceDir)                
        btnC.Bind(wx.EVT_BUTTON, self.OnExecute)  
    
  def onChioceFile(self, event):    """    按钮事件选择一个Excel文件    """    dlg = wx.FileDialog(        self, message="Choose a file",        defaultFile="",        wildcard=wildcard,        style=wx.FLP_OPEN | wx.FD_MULTIPLE | wx.DD_CHANGE_DIR    )    if dlg.ShowModal() == wx.ID_OK:        tmp = ""        paths = dlg.GetPaths()        # print(paths)        for path in paths:            tmp = tmp + path        self.Txt_Input.SetValue(tmp)    dlg.Destroy()
def onChioceFile(self, event):
    """
    按钮事件选择一个Excel文件
    """
    dlg = wx.FileDialog(
        self, message="Choose a file",
        defaultFile="",
        wildcard=wildcard,
        style=wx.FLP_OPEN | wx.FD_MULTIPLE | wx.DD_CHANGE_DIR
    )
    if dlg.ShowModal() == wx.ID_OK:
        tmp = ""
        paths = dlg.GetPaths()
        # print(paths)
        for path in paths:
            tmp = tmp + path
        self.Txt_Input.SetValue(tmp)
    dlg.Destroy()
def OnChioceDir(self, event):
    """
    按钮事件选择文件夹
    :param event:
    :return:
    """
    dlg = wx.DirDialog(self, u"选择文件夹", style=wx.DD_DEFAULT_STYLE)
    if dlg.ShowModal() == wx.ID_OK:
        path = dlg.GetPath() # 文件夹路径
        self.Txt_Output.SetValue(path)
    dlg.Destroy()
def OnExecute(self, event):
    """
    获取Excel文件并转换格式
    :param event:
    :return:
    """
    out_path = self.Txt_Output.GetValue()
    in_file = self.Txt_Input.GetValue()
    sheets = pd.read_excel(in_file)
    dir_name, file_name = os.path.split(in_file)
    shot_name,extension = os.path.splitext(file_name)
    if out_path:
        currentPath = os.path.join(out_path, shot_name + '.csv')
    else:
        currentPath = os.path.join(BASE_DIR,'resources\OutPuts', shot_name + '.csv')
    dt = pd.DataFrame(sheets)
    dt.to_csv(currentPath)
    self.Destroy()
app = wx.App(False)
frame = WxForms()
frame.Show(True)
app.MainLoop()