最近在学习python,抽空看了看图形化模块wx,顺手写了个随机密码生成器,程序运行界面如下图:(注:在Ubuntu下运行结果)
源代码如下:
- import wx
- import string
- import random
-
- class Frame(wx.Frame):
- def __init__(self,parent,id):
- wx.Frame.__init__(self,parent,id,'随机密码生成器',size=(430,285))
- panel = wx.Panel(self)
- font=wx.Font(9,wx.MODERN, wx.NORMAL, wx.NORMAL)
- wx.StaticBox(panel, -1, '参数选项', (5,10), (300,80))
- wx.StaticText(panel,-1,'密码长度',(30,30))
- self.usertext = wx.TextCtrl(panel,-1,'8',(100,30),(40,20))
- self.usertext.SetFont(font)
- wx.StaticText(panel,-1,'密码数量',(150,30))
- self.usertext1 = wx.TextCtrl(panel,-1,'1',(220,30),(40,20))
- self.usertext1.SetFont(font)
- self.select1 = wx.CheckBox(panel, -1, '字母', (30,60))
- self.select1.SetValue(True)
- self.select2 = wx.CheckBox(panel, -1, '数字', (100,60))
- self.select2.SetValue(True)
- self.select3 = wx.CheckBox(panel, -1, '特殊字符', (170,60))
- self.select3.SetValue(False)
- self.button1 = wx.Button(panel, label='生成', pos=(320, 30), size=(80, 25))
- self.button2 = wx.Button(panel, label='清空', pos=(320, 60), size=(80, 25))
- self.usertext2 = wx.TextCtrl(panel,-1,'',(30,100),(350,150),style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
- self.usertext2.SetFont(font)
- self.Bind(wx.EVT_BUTTON, self.show, self.button1)
- self.Bind(wx.EVT_BUTTON, self.clear, self.button2)
-
- def show(self,event):
- value1 = self.usertext.GetValue()
- value2 = self.usertext1.GetValue()
- value3 = self.select1.GetValue()
- value4 = self.select2.GetValue()
- value5 = self.select3.GetValue()
- yy = '~!@#$%^&*(),<>?'
- uu=''
- bob = []
-
- if not value1 or not value2:
- wx.MessageBox('请输入密码长度和密码数量','错误')
- if value3 :
- uu = uu+string.ascii_letters
- if value4 :
- uu = uu+string.digits
- if value5 :
- uu = uu+yy
- if not uu :
- wx.MessageBox('请选择密码类型','错误')
- else :
- for i in range(int(value2)):
- tt = ''.join(random.sample(list(uu),int(value1)))
- bob.append(tt)
- self.usertext2.SetValue('\n'.join(bob))
-
- def clear(self,event):
- self.usertext2.Clear()
-
- if __name__ == '__main__':
- app = wx.PySimpleApp()
- frame = Frame(parent=None, id=-1)
- frame.Show()
- app.MainLoop()