基于python和wxPython的界面授权码生成器

  • Post author:
  • Post category:python



代码:

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()


8位结果:


多位结果:



版权声明:本文为mzl_18353516147原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。