随机数小工具(python)

import wx
import random
import string
import csv
class OOP_Create():
    def __init__(self):
        self.app=wx.App()
        self.window=wx.Frame(None,title="随机数功能",size=(400,300))
        self.panel = wx.Panel(self.window)
        self.minlenText=wx.StaticText(self.panel,label="最小长度")
        self.minlentxt=wx.TextCtrl(self.panel)
        self.maxlenText=wx.StaticText(self.panel,label="最大长度")
        self.maxlentxt=wx.TextCtrl(self.panel)
        self.chkup=wx.CheckBox(self.panel,label="包含大写字母")
        self.chklow=wx.CheckBox(self.panel,label="包含小写字母")
        self.chknum=wx.CheckBox(self.panel,label="包含数字")
        self.chkpnu=wx.CheckBox(self.panel,label="包含符号")
        self.chkno = wx.CheckBox(self.panel, label="包含序号")
        self.chkemail=wx.CheckBox(self.panel,label="包含邮箱后缀")
        self.chkfile=wx.CheckBox(self.panel,label="保存到文件")
        self.lblfile=wx.StaticText(self.panel,label="文件名及路径")
        self.txtfile=wx.TextCtrl(self.panel)
        self.lblcount=wx.StaticText(self.panel,label="数据总数")
        self.txtcount=wx.TextCtrl(self.panel,value='1')
        self.butOk=wx.Button(self.panel,label="确定")
        self.butReset=wx.Button(self.panel,label="重置")
    def layout(self):
        boxsizer1=wx.BoxSizer()
        boxsizer1.Add(self.minlenText, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer1.Add(self.minlentxt, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer1.Add(self.maxlenText,flag=wx.LEFT | wx.TOP,border=10)
        boxsizer1.Add(self.maxlentxt, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer2=wx.BoxSizer()
        boxsizer2.Add(self.chkup,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer2.Add(self.chklow, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer3=wx.BoxSizer()
        boxsizer3.Add(self.chknum,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer3.Add(self.chkpnu, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer4=wx.BoxSizer()
        boxsizer4.Add(self.chkno,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer4.Add(self.chkemail, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer5=wx.BoxSizer()
        boxsizer5.Add(self.chkfile,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer5.Add(self.lblfile, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer5.Add(self.txtfile, flag=wx.LEFT | wx.TOP, border=10)
        boxsizer6=wx.BoxSizer()
        boxsizer6.Add(self.lblcount,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer6.Add(self.txtcount,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer7=wx.BoxSizer()
        boxsizer7.Add(self.butOk,flag=wx.LEFT|wx.TOP,border=10)
        boxsizer7.Add(self.butReset,flag=wx.LEFT|wx.TOP,border=10)
        boxsizerFinal=wx.BoxSizer(wx.VERTICAL)
        for i in range(1,8):
            box="boxsizer"+str(i)
            boxsizerFinal.Add(eval(box))
        self.panel.SetSizer(boxsizerFinal)

    def eventbind(self):
        self.butOk.Bind(wx.EVT_BUTTON, self.checkinput)
        self.butReset.Bind(wx.EVT_BUTTON, self.reset)
    def reset(self, event):
        self.minlentxt.SetValue("")
        self.maxlentxt.SetValue("")
        self.chkup.SetValue(False)
        self.chklow.SetValue(False)
        self.chknum.SetValue(False)
        self.chkpnu.SetValue(False)
        self.chkno.SetValue(False)
        self.chkemail.SetValue(False)
        self.chkfile.SetValue(False)
    def checkinput(self,event):
        strtmp = ""
        self.checklen()
        chkresult=self.nonmethod()
        number=self.checknumber()
        for i in range(1,number+1):
            if chkresult==0 and number!=0:
                resultdata=self.createdata()
                strtmp=strtmp+resultdata+"\n"
                print(strtmp)
                if self.chkfile.GetValue()==False:
                    self.showdata(strtmp)
                else:
                    self.savefile(strtmp)
            elif chkresult==1 and number!=0:
                resultdata=self.createdatamethod(i)
                strtmp=strtmp+resultdata+"\n"
                print(strtmp)
                if self.chkfile.GetValue()==False:
                    self.showdata(strtmp)
                else:
                    self.savefile(strtmp)
    def checklen(self):
        self.minlen=self.minlentxt.GetValue()
        self.maxlen=self.maxlentxt.GetValue()
        if (self.minlen==""):
            dlg=wx.MessageDialog(None,"最小长度不能为空","错误信息",wx.YES_DEFAULT | wx.ICON_QUESTION)
            if dlg.ShowModal()==wx.ID_YES:
                dlg.Destroy()
                return (0)
        elif (self.maxlen==""):
            dlg=wx.MessageDialog(None,"最大长度不能为空","错误信息",wx.YES_DEFAULT | wx.ICON_QUESTION)
            if dlg.ShowModal()==wx.ID_YES:
                dlg.Destroy()
                return (0)
        elif (int(self.minlen)>int(self.maxlen)):
            dlg=wx.MessageDialog(None,"最小长度不能大于最大长度","错误信息",wx.YES_DEFAULT | wx.ICON_QUESTION)
            if dlg.ShowModal()==wx.ID_YES:
                dlg.Destroy()
                return (0)
    def nonmethod(self):
        self.chklist=[]
        if self.chkup.GetValue()==True:
            self.chklist.append('up')
        if self.chklow.GetValue()==True:
            self.chklist.append('low')
        if self.chknum.GetValue()==True:
            self.chklist.append('num')
        if self.chkpnu.GetValue()==True:
            self.chklist.append('pnu')
        if self.chkno.GetValue()==True:
            self.chklist.append('no')
        if self.chkemail.GetValue()==True:
            self.chklist.append('email')
        print(self.chklist)
        if len(self.chklist)==0:
            return (0)
        else:
            return (1)
    def createdata(self):
        num=random.randint(int(self.minlen),int(self.maxlen))
        print("数量为",num)
        str=""
        str=str+string.ascii_letters+string.ascii_letters+string.ascii_letters+string.ascii_letters
        resultdata=''.join(random.sample(str,num))
        return resultdata
    def createdatamethod(self,number):
        rexemail = ['@51testing.com', '@126.com', '@163.com', '@sina.com', '@sohu.com', '@qq.com']
        strtmp = ""
        self.no = number
        if 'up' in self.chklist:
            strtmp=strtmp+string.ascii_uppercase+string.ascii_uppercase+string.ascii_uppercase+string.ascii_uppercase+string.ascii_uppercase
        if 'low' in self.chklist:
            strtmp=strtmp+string.ascii_lowercase+string.ascii_lowercase+string.ascii_lowercase+string.ascii_lowercase+string.ascii_lowercase
        if 'num' in self.chklist:
            strtmp=strtmp+string.digits+string.digits+string.digits+string.digits+string.digits+string.digits+string.digits
        if 'pnu' in self.chklist:
            strtmp=strtmp+string.punctuation+string.punctuation+string.punctuation+string.punctuation+string.punctuation+string.punctuation
        num=random.randint(int(self.minlen),int(self.maxlen))
        if strtmp!='':
            resultdata=''.join(random.sample(strtmp,num))
        else:
            # 如果其他条件都没有选择,默认调用生成字母串
            resultdata = self.createdata()
        if 'no' in self.chklist:
            resultdata = str(self.no) + resultdata
            print("xuhao", resultdata)
        if 'email' in self.chklist:
            eml=random.choice(rexemail)
            resultdata=resultdata+eml

        return  resultdata
    def checknumber(self):
        if self.txtcount.GetValue()=='' or int(self.txtcount.GetValue())<1:
            dlg = wx.MessageDialog(None, "数据总数不能为空", "错误信息", wx.YES_DEFAULT | wx.ICON_QUESTION)
            if dlg.ShowModal() == wx.ID_YES:
                dlg.Destroy()
                return (0)
        else:
            number=int(self.txtcount.GetValue())
            return number

    def showdata(self, resultdata):
        self.windownew = wx.Frame(None, title="显示测试数据", size=(400, 300))
        # 窗口中创建一个panel
        self.panelnew = wx.Panel(self.windownew)
        # 再定义一个多行文本框
        wx.TextCtrl(self.panelnew, value=resultdata, style=wx.TE_MULTILINE, size=(400, 300))
        self.windownew.Show(True)
    def savefile(self,resultdata):
        filetmp = self.txtfile.GetValue()
        if (filetmp==""):
            dlg = wx.MessageDialog(None, "请输入文件名及路径", "错误信息", wx.YES_DEFAULT | wx.ICON_QUESTION)
            if dlg.ShowModal() == wx.ID_YES:
                dlg.Destroy()
                return (0)
        else:
            file = open(filetmp, "w", newline="")
            write = csv.writer(file)
            print("传入文件中的内容", resultdata)
            tmp = resultdata.split("\n")
            for element in tmp:
                print(element)
                write.writerow([element])
            file.close()

    def run(self):
        self.window.Show(True)
        self.app.MainLoop()
if __name__ == '__main__':
    OOP_CreateObj=OOP_Create()
    OOP_CreateObj.layout()
    OOP_CreateObj.eventbind()
    OOP_CreateObj.run()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值