wxPYTHON图形化软件开发(一)---LOMO工具箱

时间:2023-03-08 20:12:50

最近学了wxPYTHON,这次就做了一个工具箱软件练手,软件主要是包含各种小工具,目前想到的有密码管理器,日记本,记账本,今天还看到一个网页浏览器,也可能加进来。目前实现的是密码管理器

软件GUI部分纯用wxPYTHON实现,数据库管理用到了sqlite3库和shelve库

软件流程主要包括一个启动画面,一个登陆对话框,和一个主界面了。

启动画面目前比较简单,就是一张图片;

wxPYTHON图形化软件开发(一)---LOMO工具箱

(数据库对应文件:loginsql.py)登录对话框,登录对话框实现了密码登录和注册功能,后面并实现了软件的多用户使用,用户之间数据分离,这里用到了sqlite3数据库来管理:

wxPYTHON图形化软件开发(一)---LOMO工具箱

(对应文件:mainfunc.py)主界面,由于目前还没想到在菜单栏,工具栏和状态栏放什么东西就只是简单实现了一下,

(对应index.py)用户主窗口包括了主页,目前由于不知放啥好,放了张图片,加一个电子时钟:

wxPYTHON图形化软件开发(一)---LOMO工具箱

(对应文件:Keymanager.py)其他实现了各种小工具,由于是模块化管理,各工具之间并没有什么关系,后面添加新的工具比较简单,

目前第一个是密码管理器,这里与前面的登录用户是一一对应的,使用的是shelve库来管理数据,其实这个代码写的比较烂,有很多可以改进的地方:

wxPYTHON图形化软件开发(一)---LOMO工具箱

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

  (未完待续。。。)