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 周锐 阅读(751) 评论(0)  编辑  收藏 所属分类: Python

只有注册用户登录后才能发表评论。


网站导航: