最近學了wxPYTHON,這次就做了一個工具箱軟件練手,軟件主要是包含各種小工具,目前想到的有密碼管理器,日記本,記賬本,今天還看到一個網頁瀏覽器,也可能加進來。目前實現的是密碼管理器
軟件GUI部分純用wxPYTHON實現,數據庫管理用到了sqlite3庫和shelve庫
軟件流程主要包括一個啟動畫面,一個登陸對話框,和一個主界面了。
啟動畫面目前比較簡單,就是一張圖片;

(數據庫對應文件:loginsql.py)登錄對話框,登錄對話框實現了密碼登錄和注冊功能,后面並實現了軟件的多用戶使用,用戶之間數據分離,這里用到了sqlite3數據庫來管理:

(對應文件:mainfunc.py)主界面,由於目前還沒想到在菜單欄,工具欄和狀態欄放什么東西就只是簡單實現了一下,
(對應index.py)用戶主窗口包括了主頁,目前由於不知放啥好,放了張圖片,加一個電子時鍾:

(對應文件:Keymanager.py)其他實現了各種小工具,由於是模塊化管理,各工具之間並沒有什么關系,后面添加新的工具比較簡單,
目前第一個是密碼管理器,這里與前面的登錄用戶是一一對應的,使用的是shelve庫來管理數據,其實這個代碼寫的比較爛,有很多可以改進的地方:

代碼mainfunc.py:
#-*- coding=utf-8 -*-
import wx
import wx.lib.buttons as buttons
from database import loginsql
from modules import Dairy
from modules import Keymanager
from modules import index
value=0
string=''
#主框架
class newframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,u'LOMO工具箱',size=(1200,955),pos=(350,20))
self.SetMinSize((1200,955))
self.SetMaxSize((1200,955))
self.splitterwindow()
self.statusbar()
self.initindex(None)
self.cursorinit()
self.menubar()
self.toolbar()
self.panel1buttonadd()
self.panel1buttonbind()
#分割窗口
def splitterwindow(self):
self.sp=wx.SplitterWindow(self,style=wx.SP_LIVE_UPDATE)
self.panel1=wx.Panel(self.sp,-1,style=wx.SUNKEN_BORDER)
self.panel2=wx.Panel(self.sp,-1)
self.panel1.SetBackgroundColour('sky blue')
self.panel2.SetBackgroundColour('AQUAMARINE')
self.sp.SplitVertically(self.panel1,self.panel2,150)
self.sp.SetMinimumPaneSize(150)
#狀態欄
def statusbar(self):
self.statusbar=self.CreateStatusBar()
self.statusbar.SetFieldsCount(3)
self.panel2.Bind(wx.EVT_MOTION, self.OnMotion)
#工具欄
def toolbar(self):
self.toolbar=self.CreateToolBar()
#狀態欄坐標顯示
def OnMotion(self,event):
self.statusbar.SetStatusText(u'光標坐標: '+str(event.GetPositionTuple()),1)
#菜單欄
def menubar(self):
menubar=wx.MenuBar()
menu1=wx.Menu()
menu2=wx.Menu()
menu3=wx.Menu()
menubar.Append(menu1,u'文件')
menubar.Append(menu2,u'設置')
menubar.Append(menu3,u'退出')
self.SetMenuBar(menubar)
#panel1按鈕數據
def buttondata(self):
return [['./pic/homepage.png',u'主頁'],
['./pic/lock.png',u'個人人密碼管理助手'],
['./pic/diary.png',u'日記每一天']]
#panel1按鈕創建
def buttoncreate(self,index):
pic=wx.Image(self.buttondata()[index][0],wx.BITMAP_TYPE_PNG).Scale(80,80).ConvertToBitmap()
self.button=buttons.GenBitmapButton(self.panel1,-1,pic,size=(150,120))
self.button.SetBezelWidth(7)
self.button.SetBackgroundColour('CORAL')
self.button.SetToolTipString(self.buttondata()[index][1])
return self.button
#panel1按鈕添加
def panel1buttonadd(self):
self.button1=self.buttoncreate(0)
self.button2=self.buttoncreate(1)
self.button3=self.buttoncreate(2)
sizer = wx.FlexGridSizer( rows=0,cols=1, hgap=5, vgap=5)
sizer.Add(self.button1,0,wx.EXPAND)
sizer.Add(self.button2,0,wx.EXPAND)
sizer.Add(self.button3,0,wx.EXPAND)
sizer.AddGrowableCol(0, proportion=1)
sizer.Layout()
self.panel1.SetSizer(sizer)
self.panel1.Fit()
#按鈕事件綁定
def panel1buttonbind(self):
self.Bind(wx.EVT_BUTTON,self.initindex,self.button1)
self.Bind(wx.EVT_BUTTON,self.KEYhandler,self.button2)
self.Bind(wx.EVT_BUTTON,self.RIJIhandler,self.button3)
#自定義光標
def cursorinit(self):
cursorpic=wx.Image('./pic/cursor.png',wx.BITMAP_TYPE_PNG)
self.cursor=wx.CursorFromImage(cursorpic)
self.SetCursor(self.cursor)
#主頁按鈕事件
def initindex(self,event):
self.statusbar.SetStatusText(u'歡迎使用LOMO工具箱!',0)
self.Shutdowntimer()
self.index=index.indexinit(self.panel2)
#密碼本按鈕事件
def KEYhandler(self,event):
global string
self.Shutdowntimer()
self.statusbar.SetStatusText(u'這是我的密碼本,嘿嘿',0)
temp=Keymanager.Keyinit(self.panel2,string)
#日記本按鈕事件
def RIJIhandler(self,event):
global string
self.Shutdowntimer()
self.statusbar.SetStatusText(u'日記本不能偷看啊!!!',0)
Dairy.dairyinit(self.panel2,string)
#關閉index定時器
def Shutdowntimer(self):
try:
self.index.timer.Stop()
del self.index.timer
except:
pass
#啟動畫面
def splashscreen():
P1=wx.Image('./pic/splashscreen.png',type=wx.BITMAP_TYPE_PNG).ConvertToBitmap()
wx.SplashScreen(P1,wx.SPLASH_CENTER_ON_SCREEN|wx.SPLASH_TIMEOUT,1000,None,-1)
#登錄對話框
class MyDialog(wx.Dialog):
def __init__(self):
text=u'歡迎使用LOMO工具箱!'
wx.Dialog.__init__(self,None,-1,u'登錄/注冊',pos=(750,330))
#對話框部件設置
self.SetBackgroundColour('CORAL')
texts=wx.StaticText(self,-1,text)
name=wx.StaticText(self,-1,u'用戶名')
password=wx.StaticText(self,-1,u'密碼')
self.namet=wx.TextCtrl(self)
self.passwordt=wx.TextCtrl(self,style=wx.TE_PASSWORD)
self.namet.SetBackgroundColour('white')
self.passwordt.SetBackgroundColour('white')
self.blank=wx.StaticText(self,-1,'')
login=wx.Button(self,id=-1,label=u'登錄')
sign=wx.Button(self,id=-1,label=u'注冊')
login.SetBackgroundColour('sky blue')
sign.SetBackgroundColour('sky blue')
fgs=wx.FlexGridSizer(2,2,5,5)
fgs.Add(name, 0, wx.ALIGN_RIGHT)
fgs.Add(self.namet, 0, wx.EXPAND)
fgs.Add(password, 0, wx.ALIGN_RIGHT)
fgs.Add(self.passwordt, 0, wx.EXPAND)
fgs.AddGrowableCol(1)
#sizer設置
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(texts,0,wx.ALL,5)
sizer.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(self.blank,0,wx.ALIGN_CENTER, 5)
sizer.Add(login, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(sign, 0, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
#按鈕綁定
self.Bind(wx.EVT_BUTTON,self.loginhandler,login)
self.Bind(wx.EVT_BUTTON,self.signhandler,sign)
self.Bind(wx.EVT_CLOSE, self.OnCloseMe)
#登錄事件
def loginhandler(self,event):
global value,string
final=loginsql.check(self.namet.GetValue(),self.passwordt.GetValue())
if(not final):
self.blank.SetLabelText(u'用戶名或密碼錯誤')
else:
string=self.namet.GetValue()
self.Close(True)
value=0
event.Skip()
#注冊事件
def signhandler(self,event):
returns=loginsql.insert(self.namet.GetValue(),self.passwordt.GetValue())
if returns:
self.blank.SetLabelText(u'用戶名或密碼不能為空')
#關閉事件
def OnCloseMe(self, event):
global value
value=1
self.Close(True)
event.Skip()
if __name__ == '__main__':
newapp=wx.App(False)
#啟動畫面
splashscreen()
#登錄對話框
dlg=MyDialog()
dlg.ShowModal()
dlg.Destroy()
if value:
exit()
#主框架
frame=newframe()
frame.Show()
newapp.MainLoop()
代碼index.py
#-*- coding:utf-8 -*-
import wx
import wx.lib.buttons as buttons
import time
import wx.gizmos as gizmos
class indexinit():
def __init__(self,panel2):
self.panel2=panel2
self.initpanel2(None)
#初始化
def initpanel2(self,event):
self.panel2.DestroyChildren()
self.panelpic=wx.Image('./pic/panel.jpg',wx.BITMAP_TYPE_JPEG,).Scale(1050,955).ConvertToBitmap()
self.picbk=wx.StaticBitmap(parent=self.panel2,pos=(0,0),bitmap=self.panelpic)
self.LEDinit()
#LED顯示器
def LEDinit(self):
led = gizmos.LEDNumberCtrl(self.picbk, -1, (700,30), (250, 50),gizmos.LED_ALIGN_CENTER)#| gizmos.LED_DRAW_FADED)
led.SetForegroundColour('black')
led.SetBackgroundColour('yellow green')
self.clock = led
self.OnTimer(None)
self.timer = wx.Timer()
self.timer.Start(1000)
self.timer.Bind(wx.EVT_TIMER, self.OnTimer)
#刷新器
def OnTimer(self, evt):
t = time.localtime(time.time())
st = time.strftime("%I-%M-%S", t)
self.clock.SetValue(st)
代碼:loginsql.py:
#-*- coding=utf-8 -*-
import sqlite3
import wx
#登錄檢查
def check(name,password):
conn = sqlite3.connect('./data/user.db')
c = conn.cursor()
try:
passwordr=c.execute('SELECT password FROM USER WHERE username=? ',[(name)])
a=''
for i in passwordr:
a=str(i[0])
if(a==password and a is not ''):
return 1
else:
return 0
except sqlite3.OperationalError:
return 0
finally:
conn.close()
#注冊事件
def insert(name,password):
print name,password
if(name=='' or password==''):
return 1
conn=sqlite3.connect('./data/user.db')
c = conn.cursor()
c.execute('INSERT INTO USER (username,password) VALUES(?,?)',(name,password))
conn.commit()
conn.close()
return 0
#建表事件,用於測試
def create():
conn=sqlite3.connect('./data/user.db')
c = conn.cursor()
c.execute('CREATE TABLE USER (username VARCHAR PRIMARY KEY NOT NULL, password VARCHAR NOT NULL);')
conn.close()
#刪除事件,用於測試
def delete(name):
conn=sqlite3.connect('./data/user.db')
c = conn.cursor()
c.execute('DELETE FROM USER WHERE name=?',[(name)])
#查表事件,用於測試
def showtable():
conn=sqlite3.connect('./data/user.db')
c=conn.cursor()
passwordr=c.execute('''SELECT password,username FROM USER ''')
for i in passwordr:
print i[0],i[1]
conn.close()
代碼Keymanager.py:
#-*- coding=utf-8 -*-
import wx
import wx.grid
import shelve
import wx.lib.buttons as buttons
class Keyinit():
def __init__(self,panel,string):
self.panel=panel
self.string=string.encode("ascii")
self.shelvefile(string)
panel.DestroyChildren ()
self.buttonadd()
self.buttonbind()
self.tableinit()
#表格初始化
def tableinit(self):
self.collabels=[u'項目名',u'用戶名',u'密碼',u'注釋']
self.grid = wx.grid.Grid(parent=self.panel,id=-1,pos=wx.DefaultPosition,size=(1027,700),style=wx.WANTS_CHARS,)
self.grid.CreateGrid(45,4)
self.grid.SetColSize(1,200)
self.grid.SetDefaultColSize(230,True)
self.grid.SetDefaultRowSize(35,True)
self.grid.SetDefaultCellAlignment(wx.CENTRE, wx.CENTRE)
self.grid.SetDefaultCellTextColour('black')
self.grid.SetDefaultCellBackgroundColour('sky blue')
self.grid.SetDefaultCellFont(wx.Font(11,wx.SWISS,wx.NORMAL,wx.NORMAL))
for row in range(4):
self.grid.SetColLabelValue(row,self.collabels[row])
self.showtable()
#按鈕數據
def buttondata(self):
return [['./pic/save.png',u'保存',(550,720)],
['./pic/plus.png',u'新建',(780,720)]]
#panel1按鈕創建
def buttoncreate(self,index):
pic=wx.Image(self.buttondata()[index][0],wx.BITMAP_TYPE_PNG).Scale(65,65).ConvertToBitmap()
self.button=buttons.GenBitmapButton(self.panel,-1,pic,size=(90,90),pos=self.buttondata()[index][2])
self.button.SetBackgroundColour('CORAL')
self.button.SetToolTipString(self.buttondata()[index][1])
return self.button
#panel1按鈕添加
def buttonadd(self):
self.button1=self.buttoncreate(0)
self.button2=self.buttoncreate(1)
#按鈕事件綁定
def buttonbind(self):
self.button1.Bind(wx.EVT_BUTTON,self.savehandler)
self.button2.Bind(wx.EVT_BUTTON,self.newhandler)
#數據庫查詢
def shelvefile(self,string):
self.database=shelve.open('./data/database.dat')
try:
self.person=self.database[self.string]
except:
self.database[self.string]=[[None for i in range(4)] for j in range(45)]
#顯示表
def showtable(self):
for row in range(len(self.database[self.string])):
for col in range(4):
temp=self.database[self.string][row][col]
if temp is not None:
self.grid.SetCellValue(row, col,'%s' % self.database[self.string][row][col])
else:
self.grid.SetCellValue(row, col,'')
self.grid.ForceRefresh()
#保存事件
def savehandler(self,event):
temp=self.database[self.string]
for i in range(self.grid.GetNumberRows()):
for j in range(self.grid.GetNumberCols()):
temp[i][j]=self.grid.GetCellValue(i,j)
self.database[self.string]=temp
self.showtable()
#新建事件
def newhandler(self,event):
self.grid.AppendRows(numRows=1)
self.grid.ForceRefresh()
(未完待續。。。)
