def crete_edit_ctrl(self, editable, par, parent, val, validator): """ Creates the edit control for a parameter :param editable: flag to indicate if the parameter is editable :param par: parameter name :param parent: parent of the control :param val: value of the parameter :param validator: validator of the control :return: a wx control object (subclass of wx.Control)? """ if type(validator) == type([]): # There should be a list of choices validator = validator[:] ctrl = wx.Choice(parent, -1, choices=validator) # Since we work with strings we have to find the right # strings positons to initilize the choice box. pos = 0 if type(val) == type(''): pos = validator.index(val) elif type(par) == type(1): pos = par ctrl.SetSelection(pos) elif isinstance(validator, bool): # Parameter is a boolean ctrl = wx.CheckBox(self, -1) ctrl.SetValue(val) # Create a non-editable box if needed # ctrl.SetEditable(editable) elif isinstance(validator, int): # Parameter is an integer ctrl = IntCtrl(self, -1, val) # Create a non-editable box if needed ctrl.SetEditable(editable) if not editable: ctrl.SetBackgroundColour(self.not_editable_bkg_color) # Otherwise it should be a validator ... else: validator = validator.Clone() ctrl = wx.TextCtrl(parent, -1, str(val), validator=validator, style=wx.TE_RIGHT | wx.TE_RICH) # Create a non-editable box if needed ctrl.SetEditable(editable) if not editable: ctrl.SetBackgroundColour(self.not_editable_bkg_color) return ctrl
class StockRegisterPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent, -1, style=wx.CLIP_CHILDREN) # 计算日期选择控件的范围 now = wx.DateTime.Today() beginDate = now.Subtract(wx.DateSpan(months=6)) labelfont = wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) bs1 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(self, wx.ID_ANY, '信息登记') label.SetFont(labelfont) bs1.Add(label, 0, wx.ALL | wx.BOTTOM, 0) gbSizer = wx.GridBagSizer(5, 5) label = wx.StaticText(self, wx.ID_ANY, '名称:') self.tc_goodsname = wx.TextCtrl(self, wx.ID_ANY, '', style=wx.TE_READONLY) gbSizer.Add(label, (0, 0), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.tc_goodsname, (0, 1), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '物品数量:') self.tc_goodsNum = IntCtrl(self) self.tc_goodsNum.SetMin(1) self.tc_goodsNum.SetNoneAllowed(True) self.tc_goodsNum.SetValue(None) self.tc_goodsNum.SetMaxLength(12) gbSizer.Add(label, (0, 2), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.tc_goodsNum, (0, 3), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '类别:') self.tc_goodcatagory = wx.TextCtrl(self, wx.ID_ANY, '', style=wx.TE_READONLY) gbSizer.Add(label, (1, 0), (1, 1), wx.ALL, 5) gbSizer.Add(self.tc_goodcatagory, (1, 1), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '经办人:') self.tc_op = wx.TextCtrl(self, wx.ID_ANY, '') self.tc_op.SetMaxLength(10) gbSizer.Add(label, (1, 2), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.tc_op, (1, 3), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '规格:') self.tc_goodsunit = wx.TextCtrl( self, wx.ID_ANY, '', ) self.tc_goodsunit.SetMaxLength(6) gbSizer.Add(label, (2, 0), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.tc_goodsunit, (2, 1), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '经办地点:') self.tc_address = wx.TextCtrl(self, wx.ID_ANY, '') self.tc_address.SetMaxLength(24) gbSizer.Add(label, (2, 2), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.tc_address, (2, 3), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '单价:') self.tc_goodsprice = wx.TextCtrl( self, wx.ID_ANY, '', ) self.tc_goodsprice.SetMaxLength(12) self.tc_goodsprice.Bind(wx.EVT_CHAR, self.OnFloatChar) gbSizer.Add(label, (3, 0), (1, 1), wx.ALL, 5) gbSizer.Add(self.tc_goodsprice, (3, 1), (1, 1), wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '登记日期:') self.op_date = wx.adv.DatePickerCtrl(self, size=(120, -1), style=wx.adv.DP_DROPDOWN | wx.adv.DP_SHOWCENTURY) #self.Bind(wx.adv.EVT_DATE_CHANGED, self.OnDateChanged, self.op_date) self.op_date.SetRange(beginDate, wx.DateTime.Today()) gbSizer.Add(label, (3, 2), (1, 1), wx.ALL | wx.ALIGN_RIGHT, 5) gbSizer.Add(self.op_date, (3, 3), (1, 1), wx.ALL | wx.EXPAND, 5) gbSizer.AddGrowableCol(1) gbSizer.AddGrowableCol(3) bs2 = wx.BoxSizer(wx.HORIZONTAL) bs2.AddStretchSpacer(1) b = buttons.GenButton(self, -1, "入库") self.Bind(wx.EVT_BUTTON, self.OnInStock, b) bs2.Add(b, 0, wx.ALL, 5) b = buttons.GenButton(self, -1, "出库") self.Bind(wx.EVT_BUTTON, self.OnOutStock, b) bs2.Add(b, 0, wx.ALL, 5) b = buttons.GenButton(self, -1, "重置") self.Bind(wx.EVT_BUTTON, self.OnReset, b) bs2.Add(b, 0, wx.ALL, 5) bs2.AddStretchSpacer(1) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(bs1, 0, wx.ALL, 5) line = wx.StaticLine(self, -1, size=(20, -1), style=wx.LI_HORIZONTAL) sizer.Add(line, 0, wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.TOP, 0) sizer.Add(gbSizer, 0, wx.ALL | wx.EXPAND, 5) sizer.Add(bs2, 0, wx.ALL | wx.EXPAND, 0) bs3 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(self, wx.ID_ANY, '出入库记录') label.SetFont(labelfont) bs3.Add(label, 0, wx.ALL, 0) sizer.Add(bs3, 0, wx.ALL, 5) line1 = wx.StaticLine(self, -1, size=(20, -1), style=wx.LI_HORIZONTAL) sizer.Add(line1, 0, wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.TOP, 0) bs4 = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText(self, wx.ID_ANY, '物品名称:') self.s_goodsname = wx.TextCtrl(self, wx.ID_ANY, size=(120, -1), style=wx.TE_PROCESS_ENTER) self.s_goodsname.SetMaxLength(16) self.Bind(wx.EVT_TEXT_ENTER, self.OnTextEnter, self.s_goodsname) bs4.Add(label, 0, wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 5) bs4.Add(self.s_goodsname, 0, wx.ALL | wx.EXPAND, 5) label = wx.StaticText(self, wx.ID_ANY, '登记日期:') self.registerDate = wx.adv.DatePickerCtrl(self, size=(120, -1), style=wx.adv.DP_DROPDOWN | wx.adv.DP_SHOWCENTURY) self.registerDate.SetRange( wx.DateTime.Today().Subtract(wx.DateSpan(months=6)), wx.DateTime.Today()) self.Bind(wx.adv.EVT_DATE_CHANGED, self.OnDateChanged, self.registerDate) bs4.Add(label, 0, wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 5) bs4.Add(self.registerDate, 0, wx.ALL | wx.EXPAND, 5) cBtn = wx.ContextHelpButton(self) cBtn.SetHelpText("默认显示当天登记的记录,并且可以对记录进行删除和修改操作。") bs4.Add(cBtn, 0, wx.ALL | wx.EXPAND, 5) bs5 = wx.BoxSizer(wx.VERTICAL) self.list = SockListCtrl(self, -1, style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.BORDER_NONE | wx.LC_HRULES | wx.LC_VRULES) self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick) #self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list) #self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list) bs5.Add(self.list, 1, wx.ALL | wx.EXPAND, 5) sizer.Add(bs4, 0, wx.ALL | wx.EXPAND, 5) sizer.Add(bs5, 1, wx.ALL | wx.EXPAND, 5) self.SetSizer(sizer) self.goodsId = None self.currentItem = None model.goodsDeatilModel.addListener(self.OnUpdate) self.queryStocksByDate() def queryStocksByDate(self): registerDate = self.registerDate.GetValue().Format('%Y-%m-%d') goodsName = self.s_goodsname.GetValue().strip() result = stockservice.get_stocks({ 'goods_name': goodsName, 'startdate': registerDate }) self.setData(result) def OnDateChanged(self, evt): self.queryStocksByDate() def OnTextEnter(self, evt): self.queryStocksByDate() def OnRightClick(self, evt): # 只第一次绑定事件 if not hasattr(self, "removeId"): self.removeId = wx.NewIdRef() self.editId = wx.NewIdRef() self.Bind(wx.EVT_MENU, self.OnDelete, id=self.removeId) #self.Bind(wx.EVT_MENU, self.OnEdit, id=self.editId) menu = wx.Menu() menu.Append(self.removeId, "删除") #menu.Append(self.editId, "编辑") self.PopupMenu(menu) menu.Destroy() # def OnEdit(self, evt): # if self.currentItem is not None: # self.list.EditLabel(self.currentItem) # else: # wx.MessageBox('请选择一条记录', '温馨提示', wx.OK_DEFAULT|wx.ICON_WARNING) # # def OnItemSelected(self, event): # self.list.OpenEditor(5, event.Index) def OnUpdate(self, m): self.tc_goodsname.SetValue(m.data['GOODS_NAME']) self.tc_goodsunit.SetValue(m.data['GOODS_UNIT']) self.tc_goodsprice.SetValue(str(m.data['GOODS_PRICE'])) self.tc_goodcatagory.SetValue(m.data['CATAGORY_NAME']) self.goodsId = m.data['ID'] def OnInStock(self, evt): self.SaveStock(1) def OnOutStock(self, evt): self.SaveStock(0) def SaveStock(self, stock_type): if self.goodsId is None: wx.MessageBox('请在左侧选择要登记的物品!', '温馨提示', wx.YES_DEFAULT | wx.ICON_EXCLAMATION) return # 输入值校验 goodnum = self.tc_goodsNum.GetValue() if goodnum is None: wx.MessageBox('请输入物品数量!', '温馨提示', wx.YES_DEFAULT | wx.ICON_EXCLAMATION) self.tc_goodsNum.SetBackgroundColour("pink") self.tc_goodsNum.SetFocus() self.tc_goodsNum.Refresh() return elif goodnum < 1: wx.MessageBox('物品数量至少为1', '温馨提示', wx.YES_DEFAULT | wx.ICON_EXCLAMATION) self.tc_goodsNum.SetBackgroundColour("pink") self.tc_goodsNum.SetFocus() self.tc_goodsNum.Refresh() return else: self.tc_goodsNum.SetBackgroundColour( wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW)) self.tc_goodsNum.Refresh() goodunit = self.tc_goodsunit.GetValue().strip() if goodunit is None or goodunit == '': wx.MessageBox('请输入物品规格!', '温馨提示', wx.YES_DEFAULT | wx.ICON_EXCLAMATION) self.tc_goodsunit.SetBackgroundColour("pink") self.tc_goodsunit.SetFocus() self.tc_goodsunit.Refresh() return else: self.tc_goodsunit.SetBackgroundColour( wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW)) self.tc_goodsunit.Refresh() stock_date = self.op_date.GetValue().Format('%Y-%m-%d') goods_id = self.goodsId goodunit = self.tc_goodsunit.GetValue().strip() goodprice = self.tc_goodsprice.GetValue().strip() if goodprice != '': pattern = re.compile('^[0-9]*\.?[0-9]{,2}$') if re.match(pattern, goodprice) is None: wx.MessageBox('单价不符合要求的格式', '温馨提示', wx.YES_DEFAULT | wx.ICON_EXCLAMATION) return goods_num = self.tc_goodsNum.GetValue() op_person = self.tc_op.GetValue().strip() op_area = self.tc_address.GetValue().strip() params = { 'STOCK_TYPE': stock_type, 'STOCK_DATE': stock_date, 'GOODS_ID': goods_id, 'GOODS_NUM': goods_num, 'GOODS_UNIT': goodunit, 'GOODS_PRICE': goodprice, 'OP_PERSON': op_person, 'OP_AREA': op_area } stockservice.add_stock(params) wx.MessageBox('物品登记成功!', '温馨提示', wx.OK | wx.ICON_INFORMATION) self.queryStocksByDate() def OnDelete(self, event): count = self.list.SelectedItemCount if count < 1: dial = wx.MessageDialog(self, '请选择要删除的记录!', '温馨提示', wx.OK) dial.ShowModal() dial.Destroy() else: dlg = wx.MessageDialog(None, u"确定要删除该条记录吗?删除将不可恢复", u"温馨提示", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: index = self.list.GetFirstSelected() item = self.list.GetItem(index) stockId = item.GetData() result = stockservice.delete_stock(stockId) if result > 0: self.list.DeleteItem(index) dial = wx.MessageDialog(self, '记录已经成功删除!', '温馨提示', wx.OK) dial.ShowModal() dial.Destroy() dlg.Destroy() def setData(self, data): self.list.DeleteAllItems() self.list.data = data for idx, row in enumerate(data): index = self.list.InsertItem(self.list.GetItemCount(), str(idx + 1)) for col, text in enumerate(row[1:]): if not text: text = '' if col == 0: text = '入库' if text == 1 else '出库' self.list.SetItem(index, col + 1, str(text)) self.list.SetItemData(index, row[0]) self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE) def OnFloatChar(self, evt): key = evt.GetKeyCode() if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255 or key == 46: evt.Skip() return if chr(key).isdigit(): evt.Skip() return return def OnReset(self, evt): self.goodsId = None self.tc_goodsname.SetValue('') self.tc_goodsunit.SetValue('') self.tc_goodsprice.SetValue('') self.tc_goodcatagory.SetValue('') self.tc_goodsNum.SetValue(None) self.tc_op.SetValue('') self.tc_address.SetValue('') self.op_date.SetValue(wx.DateTime.Today())