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: