posts - 431,  comments - 344,  trackbacks - 0
          今天幫叔叔家小弟寫了一個禮盒編號生成器,具體要求如下:


          編碼要求:
          1、編碼中不出現“4”、“3”等不吉利數字,缺碼編號;
          2、賣品第一個字母“L”表示禮盒裝、“B”表示標準裝。第二個字母是“A”到“Z”中的任意字母,可根據編碼需要任意選擇。四位數字為編號;
          3、賣品與贈品區別在于:賣品以“L”或者“B”開頭,贈品以“ZL”或者“ZB”開頭。

          例如:
          賣品禮盒裝編碼  L-A0001  L-D0001等
          賣品標準裝編碼  B-A0001  B-H0888等
          贈品禮盒裝編碼  ZL-A0001  ZL-D0001等
          贈品標準裝編碼  ZB-A0001  ZB-H0888等
           
          其中如果有3個3的號碼也留下來,不用去掉。 本來想用js來實現,但如果那樣只能把生成的結果寫給頁面上了,而且刷新就沒了。

          后來考慮了一下,選擇使用wxPython加上py2exe來做一個小的可執行文件,并把生成的編號放入txt文件中。 由于之前沒玩過wxPython,花了1個多小時終于搞定了!代碼如下:

          #encoding=utf-8
          from __future__ import with_statement
          import wx

          class ChoiceFrame(wx.Frame):
              def __init__(self):
                  wx.Frame.__init__(self, None, -1, u'編碼生成器', size=(450, 150))
                  icon=wx.EmptyIcon()
                  icon.LoadFile("SetupIcon.ico",wx.BITMAP_TYPE_ICO)
                  self.SetIcon(icon)
                  panel = wx.Panel(self, -1)
                 
                  typeList = [u'賣品禮盒裝',u'賣品標準裝',u'贈品禮盒裝',u'贈品標準裝',]
                  wx.StaticText(panel, -1, u"選擇種類:", pos=(10, 20))
                  self.type = wx.Choice(panel, -1, (85, 20), choices=typeList)
                  self.type.SetSelection(0)
                 
                  sampleList = [chr(i) for i in range(65,91)]
                  wx.StaticText(panel, -1, u"選擇字母:", pos=(210, 20))
                  self.choice = wx.Choice(panel, -1, (285, 20), choices=sampleList)
                  self.choice.SetStringSelection('A')
                 
                  wx.StaticText(panel, -1, u"起始數字:", pos=(10, 50))
                  self.start = wx.TextCtrl(panel, -1, "", pos=(80, 50))
                 
                  wx.StaticText(panel, -1, u"結束數字:", pos=(210, 50))
                  self.end = wx.TextCtrl(panel, -1, "", pos=(280, 50))
                 
                  funbtn = wx.Button(panel, -1, u"運行", pos=(280, 90))
                  self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
                 
                  self.message = wx.StaticText(panel, -1, "", pos=(10, 90))
                  self.message.SetForegroundColour("red")
                 
              def OnFunButton(self, evt):
                  self.message.SetLabel('')
                  startNum = self.start.GetValue().strip()
                  endNum = self.end.GetValue().strip()
                  if not startNum.isdigit():
                      self.message.SetLabel(u'*開始數字必須輸入整整數')
                      return
                  if not endNum.isdigit():
                      self.message.SetLabel(u'*結束數字必須輸入整整數')
                      return
                  snum = int(startNum)
                  enum = int(endNum)
                  if enum <= snum:
                      self.message.SetLabel(u'*結束數字必須大于開始數字')
                      return
                  if enum >= 10000:
                      self.message.SetLabel(u'*結束數字必須小于10000')
                      return
                  type = self.type.GetSelection()
                  choice = self.choice.GetStringSelection()

                  if (type == 0):
                      type = "L-"
                  elif (type == 1):
                      type = "B-"
                  elif (type == 2):
                      type = "ZL-"
                  elif (type == 3):
                      type = "ZB-"
                 
                  startCode = self.getCode(snum, type, choice)
                  endCode = self.getCode(enum, type, choice)
                     
                  with open(startCode + "-" + endCode + ".txt", 'w') as f:
                      for i in range(snum, enum+1):
                          code = self.getCode(i, type, choice)
                         
                          num4 = code.count('4')
                          if num4 > 0:
                              continue
                          num3 = code.count('3')
                          if num3 >0 and num3 < 3:
                              continue
                          f.write(code)
                          f.write('\n')
                  self.message.SetLabel(u'編號生成成功!!!')
                 
              def getCode(self, i, type, choice):
                  code = i
                  if i < 10:
                      code = '000' + str(i)
                  elif i < 100:
                      code = '00' + str(i)
                  elif i < 1000:
                      code = '0' + str(i)
                  return type + choice+ str(code)
             
          if __name__ == '__main__':
              app = wx.PySimpleApp()
              ChoiceFrame().Show()
              app.MainLoop()

           

          生成exe的代碼如下:

          #encoding=utf-8
          from distutils.core import setup
          import py2exe

          options = {"py2exe":  
                      {   "compressed": 1,  
                          "bundle_files": 1
                      }  
                    } 
                   
          setup(
              options = options,
              zipfile = None,
              windows=[{"script": "GeneratorCode.py", "icon_resources": [(1, "SetupIcon.ico")]}]
          )


          posted on 2009-08-19 22:30 周銳 閱讀(786) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 封开县| 望都县| 南开区| 吉林省| 涞水县| 广州市| 武乡县| 寻甸| 庆元县| 高州市| 夏河县| 满城县| 赞皇县| 昆山市| 灌云县| 周口市| 肥城市| 临武县| 临清市| 铁岭市| 八宿县| 喜德县| 安多县| 汤阴县| 如皋市| 鞍山市| 洛扎县| 江阴市| 天全县| 元氏县| 荣昌县| 阿拉善左旗| 临潭县| 石城县| 江门市| 娄烦县| 沛县| 龙游县| 涡阳县| 通许县| 高州市|