有时候我们需要为自己的产品设置一些使用权限 ,这就需要随机授权码生成器。当然这是简单的随机生成器,像Adobe这种授权码是需要一定的加密算法生成,然后再验证授权码是否正确,而不是简单的生成。
本文主要介绍wxpython中简单控件的使用和随机字符串的生成,包括:
①文本框
self.text = wx.TextCtrl(panel, wx.ID_ANY, pos=(0, 50), size=(484, 205), style=wx.TE_MULTILINE)
②按钮
button = wx.Button(panel, wx.ID_ANY, pos=(100, 255), size=(150, 50), label='生成')
button.Bind(wx.EVT_BUTTON, self.GenPassword)
③静态文字
rev = wx.StaticText(panel, -1, "生成的密码长度:", pos=(5, 10))
rev.SetForegroundColour("black")
rev.SetBackgroundColour("") # 文字背景颜色,不输入为透明
④选择框
self.cb1 = wx.CheckBox(panel, label='区分大小写', pos=(380, 10))
self.cb1.SetValue(1)
⑤随机字符串用列表推导式即可
主要功能:生成指定长度的授权码,区分大小写,查找上一个
代码:
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 |
import random import string import wx class InfoPanel(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, id, "GenCode", pos=(0, 0), size=(500, 380)) panel = wx.Panel(self, -1) rev = wx.StaticText(panel, -1, "生成的密码长度:", pos=(5, 10)) rev.SetForegroundColour("black") rev.SetBackgroundColour("") # 文字背景颜色,不输入为透明 button = wx.Button(panel, wx.ID_ANY, pos=(100, 255), size=(150, 50), label='生成') button.Bind(wx.EVT_BUTTON, self.GenPassword) button2 = wx.Button(panel, wx.ID_ANY, pos=(250, 255), size=(150, 50), label='清空') button2.Bind(wx.EVT_BUTTON, self.Clear) button3 = wx.Button(panel, wx.ID_ANY, pos=(280, 5), size=(80, 30), label='复制到剪切板') button3.Bind(wx.EVT_BUTTON, self.OnCopy) button4 = wx.Button(panel, wx.ID_ANY, pos=(100, 305), size=(300, 30), label='上一个') button4.Bind(wx.EVT_BUTTON, self.previous) self.text = wx.TextCtrl(panel, wx.ID_ANY, pos=(0, 50), size=(484, 205), style=wx.TE_MULTILINE) self.text2 = wx.TextCtrl(panel, wx.ID_ANY, pos=(110, 5), size=(150, 30)) self.text2.AppendText(str(8)) self.cb1 = wx.CheckBox(panel, label='区分大小写', pos=(380, 10)) self.cb1.SetValue(1) self.count = 0 self.genPwd = "" self.box = [] def GenPassword(self, event): try: self.text.Clear() length = int(self.text2.GetValue()) numOfNum = random.randint(1, length - 1) numOfLetter = length - numOfNum # 选中numOfNum个数字 slcNum = [random.choice(string.digits) for i in range(numOfNum)] # 选中numOfLetter个字母 slcLetter = [random.choice(string.ascii_letters) for i in range(numOfLetter)] # 打乱这个组合 slcChar = slcNum + slcLetter random.shuffle(slcChar) # 生成密码 self.genPwd = ''.join([i for i in slcChar]) if self.cb1.GetValue() == 1: self.text.AppendText(self.genPwd) self.box.append(self.genPwd) else: self.text.AppendText(self.genPwd.lower()) self.box.append(self.genPwd.lower()) self.count += 1 except ValueError: self.text.AppendText("输入错误,请输入大于1整数!!!") def previous(self, event): self.text.Clear() try: self.text.AppendText(self.box[self.count - 2]) except IndexError: self.text.AppendText("无") def Clear(self, event): self.text.Clear() self.text2.Clear() self.count = 0 self.box = [] def OnCopy(self, event): text_obj = wx.TextDataObject() text_obj.SetText(self.text.GetValue()) if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open(): wx.TheClipboard.SetData(text_obj) wx.TheClipboard.Close() dlg = wx.MessageDialog(self, '已经复制到剪切板', '复制到剪切板', wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() class MainApp(wx.App): def OnInit(self): self.frame = InfoPanel(None, -1) self.frame.Show(True) self.SetTopWindow(self.frame) return True if __name__ == '__main__': app = MainApp(0) app.MainLoop() |
效果图: