-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
147 lines (119 loc) · 5.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
import wx
from wx import grid as wxgrid
import exceldata
import logging
import os
'''
mzh
2015-4-28
'''
ID_BTN_IMPORT = wx.NewId()
ID_BTN_EXPORT = wx.NewId()
ID_PAGE_SALES = wx.NewId()
ID_PAGE_PAYMENT = wx.NewId()
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
logging.basicConfig(filename=os.path.join(os.getcwd(), 'log.txt'), level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - [%(lineno)s] - %(message)s')
self.logger = logging.getLogger('main')
self.logger.setLevel(logging.INFO)
self.logger.info('===============================module [main]=================')
self.error_msg = ''
# self.data = exceldata.get_data()
self.initUI()
def initUI(self):
self.logger.info('init main UI')
panel = wx.Panel(self)
main_box = wx.BoxSizer(wx.VERTICAL)
'''button'''
btn_box = wx.BoxSizer(wx.HORIZONTAL)
self.btn_import = wx.Button(panel, label=u'导入', size=(80, 35), id=ID_BTN_IMPORT)
self.btn_import.Bind(wx.EVT_BUTTON, self.on_button_clicked)
btn_box.Add(self.btn_import, 1)
self.btn_export = wx.Button(panel, label=u'导出', size=(80, 35), id=ID_BTN_EXPORT)
self.btn_export.Bind(wx.EVT_BUTTON, self.on_button_clicked)
btn_box.Add(self.btn_export, 1, wx.LEFT, 10)
# proportion is very import
main_box.Add(btn_box, proportion=0, flag=wx.BOTTOM|wx.LEFT, border=10)
main_box.Add((-1, 10))
'''create notebook, add a scroll panel'''
self.notebook = wx.Notebook(panel, style=wx.NB_LEFT)
self.sales_page = wx.ScrolledWindow(self.notebook, ID_PAGE_SALES, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL | wx.VSCROLL)
self.sales_grid = wxgrid.Grid(self.sales_page)
self.notebook.AddPage(self.sales_page, u"销售统计", select=True)
self.payment_page = wx.ScrolledWindow(self.notebook, ID_PAGE_PAYMENT, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL | wx.VSCROLL)
self.payment_grid = wxgrid.Grid(self.payment_page)
self.notebook.AddPage(self.payment_page, u"中收")
main_box.Add(self.notebook, proportion=1, flag=wx.EXPAND)
panel.SetSizer(main_box)
self.Center()
self.Show()
def on_button_clicked(self, e):
eid = e.GetId()
if eid == ID_BTN_IMPORT:
self.logger.info('--------clicked import button')
self.import_data()
elif eid == ID_BTN_EXPORT:
self.logger.info('--------clicked export button')
self.export_data()
'''导入数据'''
def import_data(self):
self.logger.info('read import file path')
import_dlg = wx.FileDialog(self, 'Open Excel Doc', '', '', 'Excel files |*.xls*', wx.FD_OPEN)
if import_dlg.ShowModal() == wx.ID_OK:
file_path = import_dlg.GetPath()
self.logger.info('begin to parse excel data')
self.data = exceldata.get_data(file_path)
if type(self.data) == unicode:
self.logger.info('import data, error msg:'+self.data)
if self.data is not None:
self.put_data_in_grid(self.sales_grid, self.data.get('sales'), self.data.get('sales_column'))
self.put_data_in_grid(self.payment_grid, self.data.get('payment'), self.data.get('payment_column'))
self.sales_grid.SetSize((1120, 500))
self.payment_grid.SetSize((1120, 500))
'''目前不支持多次导入'''
def put_data_in_grid(self, target_grid=None, grid_data=None, col_name_list=None):
self.logger.info('put data in grid')
if grid_data is None or col_name_list is None:
self.error_msg += 'oh, there is no data in your specified file'
wx.MessageBox(self.error_msg)
return
col_count = len(col_name_list)
row_count = len(grid_data)
target_grid.ClearGrid()
if target_grid.GetNumberRows() > 0:
target_grid.DeleteRows()
if target_grid.GetNumberCols() > 0:
target_grid.DeleteCols()
target_grid.CreateGrid(row_count, col_count)
'''设置列名'''
for i in xrange(col_count):
target_grid.SetColLabelValue(i, col_name_list[i])
'''填充表体'''
row_index = 0
for row_dict in grid_data.values():
for i in xrange(col_count):
cell_value = row_dict.get(col_name_list[i])
'''因为grid只接收String和Unicode,所以对其他类型进行转换'''
if type(cell_value) is not unicode:
cell_value = str(cell_value)
target_grid.SetCellValue(row_index, i, cell_value)
row_index += 1
def export_data(self):
self.logger.info('read export file path')
export_dlg = wx.FileDialog(self, 'Save excel file', '', '', 'Excel File|*.xls', wx.FD_SAVE)
if export_dlg.ShowModal() == wx.ID_OK:
file_path = export_dlg.GetPath()
page_id = self.notebook.GetCurrentPage().GetId()
if page_id == ID_PAGE_SALES:
exceldata.save_to_excel(self.data.get('sales'), file_path)
wx.MessageBox(u'销售数据导出成功', u'提示')
elif page_id == ID_PAGE_PAYMENT:
exceldata.save_to_excel(self.data.get('payment'), file_path)
wx.MessageBox(u'中收数据导出成功', u'提示')
if __name__ == '__main__':
app = wx.App()
mw = MainWindow(None, title='Statistics', size=(1150, 600), style=wx.CLOSE_BOX | wx.MINIMIZE_BOX | wx.CAPTION | wx.SYSTEM_MENU)
app.MainLoop()