­

wxPython开发之密码管理程序

不想记密码?密码全设置成一样担心安全?用别人程序担心密码泄露?看完本博客,开发一个属于自己的密码管理程序吧

我们用到的是python的wxPython界面库包

先来看下成果界面:简洁主题明确

功能:通过所属可以查询到设置的账号密码,可以保存账号密码,保存生成当前路径本地文件

 

 要想开发wxPython需要先下载对应包,打开cmd执行下列代码

 pip install -U wxPython

进入IDE进行开发,代码如下,可以直接运行查看

import wx
import os


class My(wx.Frame):

    def __init__(self, parent, title):
        super(My, self).__init__(parent, title=title, size=(600, 400))
        panel = wx.Panel(self)

        self.searchCtrl = wx.TextCtrl(panel, pos=(150, 10), size=(300, 30), style=wx.TE_CENTER)
        self.search = wx.Button(panel, label="查找", pos=(460, 12))
        self.search.Bind(wx.EVT_BUTTON, self.findAccount)

        self.label1 = wx.StaticText(panel, label="所属", pos=(40, 80))
        self.belong = wx.TextCtrl(panel, pos=(90, 76), size=(400, 30), style=wx.TE_LEFT)

        self.label2 = wx.StaticText(panel, label="账号", pos=(40, 140))
        self.account = wx.TextCtrl(panel, pos=(90, 136), size=(400, 30), style=wx.TE_LEFT)

        self.label3 = wx.StaticText(panel, label="密码", pos=(40, 200))
        self.password = wx.TextCtrl(panel, pos=(90, 196), size=(400, 30), style=wx.TE_LEFT)

        self.save = wx.Button(panel, label="保存", pos=(90, 266), size=(80, 30))
        self.save.Bind(wx.EVT_BUTTON, self.saveClicked)

        self.reset = wx.Button(panel, label="刷新", pos=(250, 266), size=(80, 30))
        self.reset.Bind(wx.EVT_BUTTON, self.clearClick)

        self.Centre()
        self.Show()
        self.Fit()

    # 保存文本
    def saveClicked(self, event):
        belong = self.belong.GetValue()
        account = self.account.GetValue()
        password = self.password.GetValue()
        if belong == "":
            digLog("所属不能为空", "错误信息提示")
        elif account == "":
            digLog("账号不能为空", "错误信息提示")
        elif password == "":
            digLog("密码不能为空", "错误信息提示")
        else:
            result = alike(self.belong.GetValue())
            if result:
                digLog("该所属已存在", "失败信息提示")
                return
            text = belong + "/" + account + "/" + password
            with open(os.getcwd() + "\pass.txt", 'a+', encoding='utf-8') as f:
                f.write("\n" + text)
                digLog("保存成功", "成功信息提示")
                self.save.Enable(False)

    # 刷新
    def clearClick(self, event):
        self.searchCtrl.Clear()
        self.belong.Clear()
        self.account.Clear()
        self.password.Clear()
        self.save.Enable(True)

    # 读取文本
    def findAccount(self, event):
        target = self.searchCtrl.GetValue()
        if target == "":
            digLog("输入框不能为空", "失败信息提示")
            return
        with open(os.getcwd() + "\pass.txt", 'a+', encoding='utf-8') as f:  # 从TXT文件中读出数据
            for line1 in f:
                if target in line1.split("/")[0]:  # 是否包含文字
                    self.belong.SetValue(line1.split("/")[0])
                    self.account.SetValue(line1.split("/")[1])
                    self.password.SetValue(line1.split("/")[2])
                    digLog("查找成功", "成功信息提示")
                    self.save.Enable(False)
                    return
            digLog("未查到", "失败信息提示")
            return


# 判断添加的账号是否存在
def alike(exist):
    with open(os.getcwd() + "\pass.txt", 'a+', encoding='utf-8') as foo:
        for line in foo.readlines():
            if exist in line.split("/")[0]:
                return True
            else:
                return False


def digLog(msg, title):
    toastone = wx.MessageDialog(None, msg, title, wx.YES_DEFAULT | wx.ICON_QUESTION)
    if toastone.ShowModal() == wx.ID_YES:  # 如果点击了提示框的确定按钮
        toastone.Destroy()  # 则关闭提示框


app = wx.App()
My(None, "保存密码程序")
app.MainLoop()

测试没问题后进行打包exe程序

打包需要用到一个库,强烈建议使用 pip 在线安装的方式来安装 PyInstaller 模块,不要使用离线包的方式来安装,因为 PyInstaller 模块还依赖其他模块,pip 在安装 PyInstaller 模块时会先安装它的依赖模块。

pip install pyinstaller

完成后进行打包,进入程序文件目录,打开cmd

执行下列命令 (-F是在dist只生成exe文件  -w是取消程序启动的命令框,-n是指定程序名,-i是指定程序图标 )

pyinstaller -F -w -n savePW -i .\ico.ico index.py

 

成功如下:

 

 生成的文件在程序文件的dist目录下,告别繁琐的密码文件吧

 

 

Tags: