码迷,mamicode.com
首页 > 数据库 > 详细

图书数据库操作界面升级

时间:2016-04-26 21:11:11      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

今年年初,我写了一篇名为《图书管理库以及操作界面》的文章(链接:http://blog.csdn.net/qq_32897527/article/details/50493313),但是由于技术问题,导致程序不是很优化,界面也不是很友好。最近得空稍微研究了下,对界面进行了升级。


1、使用表格进行数据项显示

技术分享升级前 技术分享升级后

#coding:gbk
import wx,sqlite3
import wx.lib.sheet as sheet

class find(wx.Frame):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        rel = conn.execute('SELECT * FROM library')
        re = rel.fetchall()
        
        if self.s.GetValue().encode('gbk') == '按书名':
            t=[]
            for i in re:
                if i[1] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按出版社':
            t=[]
            for i in re:
                if i[2] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按价格':
            t=[]
            for i in re:
                if i[3] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按分类':           
            t=[]
            for i in re:
                if i[4] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '查询全部':
            t = re
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '选择查询项':
            dial = wx.MessageDialog(None,'请选择查询项','结果',wx.YES_NO)
            dial.ShowModal()

    def __init__(self):
        wx.Frame.__init__(self, None, -1, title='查询数据',
                pos=(390,200),size=(410, 400),style=wx.CAPTION | wx.CLOSE_BOX)
        panel = wx.Panel(self)
        panel.SetBackgroundColour('white')

        self.s = wx.ComboBox(panel,value='选择查询项',
                choices=['按书名','按出版社','按价格','按分类','查询全部'],pos=(20,10),size=(100,20))
        self.t = wx.TextCtrl(panel,pos=(130,10),size=(185,20))

        self.cs = sheet.CSheet(panel)
        self.cs.SetLabelBackgroundColour('white')
        self.cs.SetNumberCols(4)
        self.cs.SetNumberRows(0)
        self.cs.SetSize((360,320))
        self.cs.SetPosition((20,40))
        column = ['书名','出版社','价格','类别']
        for col in range(4):
            self.cs.SetColLabelValue(col,column[col])

        btn=wx.Button(panel,label="查询",pos=(325,10),size=(50,20))
        btn.Bind(wx.EVT_BUTTON,self.load)

if __name__ == '__main__':
    app = wx.App()
    find().Show()
    app.MainLoop()
主要使用wx.lib.sheet库创建表格


2、用notebook将find、insert、delete三个程序合并到一起

原先的是find、insert、delete三个程序进行三个不同的操作,但是这样太过麻烦。所以使用notebook可以实现一个程序实现三个操作。

技术分享技术分享技术分享

#coding:gbk
import wx,sqlite3

class find(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        rel = conn.execute('SELECT * FROM library')
        t = 0
        h = 40
        if self.x > 0:
            for i in range(self.x):
                h1 = 60 + 20*i
                wx.StaticText(self, -1,'                           ',(20,h1))
                wx.StaticText(self, -1,'                           ',(40,h1))
                wx.StaticText(self, -1,'                           ',(120,h1))
                wx.StaticText(self, -1,'                           ',(220,h1))
                wx.StaticText(self, -1,'                           ',(260,h1))

        if self.s.GetValue().encode('gbk') == '按书名':
            for i in rel.fetchall():
                if i[1] == self.t.GetValue():
                    t = t + 1
                    h = h + 20
                    id = '%d' %i[0]
                    name = i[1].encode('gbk')
                    pub = i[2].encode('gbk')
                    price = i[3].encode('gbk')
                    type = i[4].encode('gbk')
                    wx.StaticText(self, -1,id,(20,h))
                    wx.StaticText(self, -1,name,(40,h))
                    wx.StaticText(self, -1,pub,(120,h))
                    wx.StaticText(self, -1,price,(220,h))
                    wx.StaticText(self, -1,type,(260,h))
            dial = wx.MessageDialog(None,'共有%d个结果'%t,'结果',wx.YES_NO)
            dial.ShowModal()
            self.x = t

        if self.s.GetValue().encode('gbk') == '按出版社':
            for i in rel.fetchall():
                if i[2] == self.t.GetValue():
                    t = t + 1
                    h = h + 20
                    id = '%d' %i[0]
                    name = i[1].encode('gbk')
                    pub = i[2].encode('gbk')
                    price = i[3].encode('gbk')
                    type = i[4].encode('gbk')
                    wx.StaticText(self, -1,id,(20,h))
                    wx.StaticText(self, -1,name,(40,h))
                    wx.StaticText(self, -1,pub,(120,h))
                    wx.StaticText(self, -1,price,(220,h))
                    wx.StaticText(self, -1,type,(260,h))
            dial = wx.MessageDialog(None,'共有%d个结果'%t,'结果',wx.YES_NO)
            dial.ShowModal()
            self.x = t

        if self.s.GetValue().encode('gbk') == '按价格':
            for i in rel.fetchall():
                if i[3] == self.t.GetValue():
                    t = t + 1
                    h = h + 20
                    id = '%d' %i[0]
                    name = i[1].encode('gbk')
                    pub = i[2].encode('gbk')
                    price = i[3].encode('gbk')
                    type = i[4].encode('gbk')
                    wx.StaticText(self, -1,id,(20,h))
                    wx.StaticText(self, -1,name,(40,h))
                    wx.StaticText(self, -1,pub,(120,h))
                    wx.StaticText(self, -1,price,(220,h))
                    wx.StaticText(self, -1,type,(260,h))
            dial = wx.MessageDialog(None,'共有%d个结果'%t,'结果',wx.YES_NO)
            dial.ShowModal()
            self.x = t

        if self.s.GetValue().encode('gbk') == '按分类':
            for i in rel.fetchall():
                if i[4] == self.t.GetValue():
                    t = t + 1
                    h = h + 20
                    id = '%d' %i[0]
                    name = i[1].encode('gbk')
                    pub = i[2].encode('gbk')
                    price = i[3].encode('gbk')
                    type = i[4].encode('gbk')
                    wx.StaticText(self, -1,id,(20,h))
                    wx.StaticText(self, -1,name,(40,h))
                    wx.StaticText(self, -1,pub,(120,h))
                    wx.StaticText(self, -1,price,(220,h))
                    wx.StaticText(self, -1,type,(260,h))
            dial = wx.MessageDialog(None,'共有%d个结果'%t,'结果',wx.YES_NO)
            dial.ShowModal()
            self.x = t

        if self.s.GetValue().encode('gbk') == '查询全部':
            for i in rel.fetchall():
                t = t + 1
                h = h + 20
                id = '%d' %i[0]
                name = i[1].encode('gbk')
                pub = i[2].encode('gbk')
                price = i[3].encode('gbk')
                type = i[4].encode('gbk')
                wx.StaticText(self, -1,id,(20,h))
                wx.StaticText(self, -1,name,(40,h))
                wx.StaticText(self, -1,pub,(120,h))
                wx.StaticText(self, -1,price,(220,h))
                wx.StaticText(self, -1,type,(260,h))
            dial = wx.MessageDialog(None,'共有%d个结果'%t,'结果',wx.YES_NO)
            dial.ShowModal()
            self.x = t

        if self.s.GetValue().encode('gbk') == '选择查询项':
            dial = wx.MessageDialog(None,'请选择查询项','结果',wx.YES_NO)
            dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        self.s = wx.ComboBox(self,value='选择查询项',
                choices=['按书名','按出版社','按价格','按分类','查询全部'],pos=(20,10),size=(100,20))
        self.t = wx.TextCtrl(self
                             ,pos=(130,10),size=(100,20))
        self.x = 0

        wx.StaticText(self, -1, "id",(20,40))
        wx.StaticText(self, -1, "书名",(40,40))
        wx.StaticText(self, -1, "出版社",(120,40))
        wx.StaticText(self, -1, "价格",(220,40))
        wx.StaticText(self, -1, "类别",(260,40))

        btn=wx.Button(self,label="查询",pos=(240,10),size=(50,20))
        btn.Bind(wx.EVT_BUTTON,self.load)

class insert(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        n1=self.t1.GetValue()
        n2=self.t2.GetValue()
        n3=self.t3.GetValue()
        n4=self.t4.GetValue()
        s=[n1,n2,n3,n4]
        conn.execute("insert into library(name,pub,price,type) values(?,?,?,?)",s)
        conn.commit()
        dial = wx.MessageDialog(None,'成功插入数据!','结果',wx.YES_NO)
        dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        st1 = wx.StaticText(self, -1, "请输入书名:",(20,10))
        st2 = wx.StaticText(self, -1, "请输入出版社:",(20, 60))
        st3 = wx.StaticText(self, -1, "请输入价格:",(20, 110))
        st4 = wx.StaticText(self, -1, "请选择分类:",(20, 160))

        self.t1 = wx.TextCtrl(self,pos=(20,30),size=(100,20))
        self.t2 = wx.TextCtrl(self,pos=(20,80),size=(100,20))
        self.t3 = wx.TextCtrl(self,pos=(20,130),size=(100,20))
        self.t4 = wx.ComboBox(self,value='未选择',
                choices=['文学','理工科','计算机','外语','其他'],pos=(20,180),size=(100,20))

        btn=wx.Button(self,label="插入",pos=(20,210),size=(100,30))
        btn.Bind(wx.EVT_BUTTON,self.load)

class delete(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        n1=self.t1.GetValue()
        s=[n1]
        conn.execute('DELETE FROM library where name=?',s)
        conn.commit()
        dial = wx.MessageDialog(None,'成功删除数据!','结果',wx.YES_NO)
        dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        st1 = wx.StaticText(self, -1, "请输入要删除的书名:",(20,20))

        self.t1 = wx.TextCtrl(self,pos=(20,40),size=(150,20))

        btn=wx.Button(self,label="删除",pos=(20,70),size=(100,30))
        btn.Bind(wx.EVT_BUTTON,self.load)
        
if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None, title="图书管理")
    nb = wx.Notebook(frame)


    nb.AddPage(find(nb), "查找图书")
    nb.AddPage(insert(nb), "插入图书")
    nb.AddPage(delete(nb), "删除图书")

    frame.Show()
    app.MainLoop()

3、界面美化

技术分享

#coding:gbk
import wx,sqlite3
import wx.lib.buttons as buttons
import wx.lib.sheet as sheet

class find(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        rel = conn.execute('SELECT * FROM library')
        re = rel.fetchall()
        
        if self.s.GetValue().encode('gbk') == '按书名':
            t=[]
            for i in re:
                if i[1] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按出版社':
            t=[]
            for i in re:
                if i[2] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按价格':
            t=[]
            for i in re:
                if i[3] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '按分类':           
            t=[]
            for i in re:
                if i[4] == self.t.GetValue():
                    t.append(i)
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '查询全部':
            t = re
            rows = len(t)
            self.cs.SetNumberRows(rows)
            for row in range(rows):
                self.cs.SetRowLabelValue(row,'%d'%t[row][0])
                for col in range(4):
                    self.cs.SetCellValue(row,col,t[row][col+1])
            dial = wx.MessageDialog(None,'共有%d个结果'%rows,'结果',wx.YES_NO)
            dial.ShowModal()

        if self.s.GetValue().encode('gbk') == '选择查询项':
            dial = wx.MessageDialog(None,'请选择查询项','结果',wx.YES_NO)
            dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        self.s = wx.ComboBox(self,value='选择查询项',
                choices=['按书名','按出版社','按价格','按分类','查询全部'],pos=(20,10),size=(100,20))
        self.t = wx.TextCtrl(self,pos=(130,10),size=(185,20))

        self.cs = sheet.CSheet(self)
        self.cs.SetLabelBackgroundColour('#ffffff')
        self.cs.SetNumberCols(4)
        self.cs.SetNumberRows(0)
        self.cs.SetSize((550,520))
        self.cs.SetPosition((20,40))
        column = ['书名','出版社','价格','类别']
        for col in range(4):
            self.cs.SetColLabelValue(col,column[col])

        btn=wx.Button(self,label="查询",pos=(325,10),size=(50,20))
        btn.Bind(wx.EVT_BUTTON,self.load)

class insert(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        n1=self.t1.GetValue()
        n2=self.t2.GetValue()
        n3=self.t3.GetValue()
        n4=self.t4.GetValue()
        s=[n1,n2,n3,n4]
        conn.execute("insert into library(name,pub,price,type) values(?,?,?,?)",s)
        conn.commit()
        dial = wx.MessageDialog(None,'成功插入数据!','结果',wx.YES_NO)
        dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        st1 = wx.StaticText(self, -1, "请输入书名:",(20,10))
        st2 = wx.StaticText(self, -1, "请输入出版社:",(20, 60))
        st3 = wx.StaticText(self, -1, "请输入价格:",(20, 110))
        st4 = wx.StaticText(self, -1, "请选择分类:",(20, 160))

        self.t1 = wx.TextCtrl(self,pos=(20,30),size=(200,20))
        self.t2 = wx.TextCtrl(self,pos=(20,80),size=(200,20))
        self.t3 = wx.TextCtrl(self,pos=(20,130),size=(200,20))
        self.t4 = wx.ComboBox(self,value='未选择',
                choices=['文学','理工科','计算机','外语','其他'],pos=(20,180),size=(200,20))

        btn=wx.Button(self,label="插入",pos=(20,210),size=(100,30))
        btn.Bind(wx.EVT_BUTTON,self.load)

class delete(wx.Panel):
    def load(self,event):
        conn=sqlite3.connect("F:\\Python\My Program\library_system\library.db")
        conn.cursor()
        n1=self.t1.GetValue()
        s=[n1]
        conn.execute('DELETE FROM library where name=?',s)
        conn.commit()
        dial = wx.MessageDialog(None,'成功删除数据!','结果',wx.YES_NO)
        dial.ShowModal()

    def __init__(self,parent):
        wx.Panel.__init__(self,parent)

        st1 = wx.StaticText(self, -1, "请输入要删除的书名:",(20,20))

        self.t1 = wx.TextCtrl(self,pos=(20,40),size=(200,20))

        btn=wx.Button(self,label="删除",pos=(20,70),size=(100,30))
        btn.Bind(wx.EVT_BUTTON,self.load)

def OnPaint(event):
    dc = wx.PaintDC(lp)
    dc.DrawBitmap(map,28,50)

def bt1(event):
    b1.SetBackgroundColour('#011b27')
    b2.SetBackgroundColour('#052b3b')
    b3.SetBackgroundColour('#052b3b')
    rp1.Show(1)
    rp2.Show(0)
    rp3.Show(0)
    frame.Refresh()

def bt2(event):
    b1.SetBackgroundColour('#052b3b')
    b2.SetBackgroundColour('#011b27')
    b3.SetBackgroundColour('#052b3b')
    rp1.Show(0)
    rp2.Show(1)
    rp3.Show(0)
    frame.Refresh()

def bt3(event):
    b1.SetBackgroundColour('#052b3b')
    b2.SetBackgroundColour('#052b3b')
    b3.SetBackgroundColour('#011b27')
    rp1.Show(0)
    rp2.Show(0)
    rp3.Show(1)
    frame.Refresh()

app = wx.App(False)
frame = wx.Frame(None, title='图书管理',size=(800,600),style=wx.CAPTION | wx.CLOSE_BOX)
frame.SetBackgroundColour('#0a3142')

rp1 = find(frame)
rp2 = insert(frame)
rp3 = delete(frame)
rp1.SetBackgroundColour('#ffffff');rp1.SetSize((600,600));rp1.SetPosition((200,0))
rp2.SetBackgroundColour('#ffffff');rp2.SetSize((600,600));rp2.SetPosition((200,0))
rp3.SetBackgroundColour('#ffffff');rp3.SetSize((600,600));rp3.SetPosition((200,0))
rp2.Show(0)
rp3.Show(0)

lp = wx.Panel(frame,-1)
lp.SetSize((200,600))
b1 = buttons.GenButton(lp, -1, '查询图书',pos=(0,250),size=(200,50))
b2 = buttons.GenButton(lp, -1, '插入图书',pos=(0,300),size=(200,50))
b3 = buttons.GenButton(lp, -1, '删除图书',pos=(0,350),size=(200,50))
b1.SetBackgroundColour('#011b27');b1.SetForegroundColour('#ffffff');b1.SetBezelWidth(1);b1.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
b1.Bind(wx.EVT_BUTTON,bt1)
b2.SetBackgroundColour('#052b3b');b2.SetForegroundColour('#ffffff');b2.SetBezelWidth(1);b2.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
b2.Bind(wx.EVT_BUTTON,bt2)
b3.SetBackgroundColour('#052b3b');b3.SetForegroundColour('#ffffff');b3.SetBezelWidth(1);b3.SetFont(wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
b3.Bind(wx.EVT_BUTTON,bt3)
map = wx.Bitmap('photo.png')
wx.EVT_PAINT(lp,OnPaint)

frame.Show()
app.MainLoop()
都是些基本的wxpython,用于装饰界面

图书数据库操作界面升级

标签:

原文地址:http://blog.csdn.net/qq_32897527/article/details/51233275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!