最近把代码重构了,主要是功能拆分,去掉臃肿的代码。
这个我写过两个版本,一个是文件操作的版本,一个是sqllite版本。暂时只修改了文件版本,等年后辞职了再来完善。
当时练习C语言的时候,我看的书中有个cmd版本的学生成绩管理系统,就想着能不能用GUI去实现,所以就做了这么个东西。
补上一张效果图
import wx
import os
import sqlite3
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,"学生成绩管理系统1.0",(400,200),(956,770),wx.SYSTEM_MENU|wx.MINIMIZE_BOX|wx.CLOSE_BOX|wx.CAPTION|wx.WS_EX_VALIDATE_RECURSIVELY)
self.InitUI()# 显示界面
self.flag = 0
self.SetBackgroundColour("cyan")
def InitUI(self):
#设置字体样式
self.font1 = wx.Font(24,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.font2 = wx.Font(28,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.font3 = wx.Font(18,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.panel = wx.Panel(self)
self.panel.Bind(wx.EVT_ERASE_BACKGROUND,self.onEraseBack)
self.Create_mainmenu()
self.ShowMenu(True)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText("This is a programe to manage the information of the students'")
self.statusbar.SetBackgroundColour("cyan")
def onEraseBack(self,event):
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("image.jpg")
dc.DrawBitmap(bmp, 0, 0)
def Create_mainmenu(self):
self.main_title = wx.StaticText(self.panel,-1,"STUDENT SYSTEM",(337,50))
self.bt_list = [wx.Button(self.panel,-1,"Exit",(150,113),(300, 100)),
wx.Button(self.panel, -1, "Input record", (490, 113), (300, 100)),
wx.Button(self.panel, -1, "Search record", (150, 258), (300, 100)),
wx.Button(self.panel, -1, "Delete record", (490, 258), (300, 100)),
wx.Button(self.panel, -1, "Modify record", (150, 403), (300, 100)),
wx.Button(self.panel, -1, "Insert record", (490, 403), (300, 100)),
wx.Button(self.panel, -1, "Order", (150, 548), (300, 100)),
wx.Button(self.panel,-1,"Number", (490,548),(300, 100))]
self.evt_list = [self.Event1,self.Event2,self.Event3,self.Event4,self.Event5,self.Event6,self.Event7,self.Event8]
# 设置字体绑定事件
self.main_title.SetFont(self.font2)
for i,j in enumerate(self.bt_list):
j.SetFont(self.font1)
j.SetBackgroundColour('aquamarine')
j.Bind(wx.EVT_BUTTON,self.evt_list[i])
def ShowMenu(self,state):
#根据参数显示或隐藏主界面
for i in self.bt_list:
i.Show(state)
def Event1(self,event):
self.Destroy()#退出整个系统
def Event2(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
self.ShowMenu(False)
self.panel.Bind(wx.EVT_ERASE_BACKGROUND, self.onEraseBack)
label_num = wx.StaticText(self.panel,pos=(0,5),label="Number")
label_name = wx.StaticText(self.panel,pos=(170,5),label="Name")
label_elec = wx.StaticText(self.panel,pos=(330,5),label="Elective")
label_expe = wx.StaticText(self.panel,pos=(490,5),label="Experiment")
label_requ = wx.StaticText(self.panel,pos=(660,5),label="Required")
label_sum = wx.StaticText(self.panel,pos=(840,5),label="Sum")
self.table = wx.TextCtrl(self.panel,size=(940,600),pos=(0,30),style=wx.TE_MULTILINE|wx.TE_READONLY)
datalist = self.Readfile()
for i in datalist:
for j in i:
self.table.AppendText(str(j)+"\t\t\t")
self.table.AppendText("\n\n")
self.bt_list2 = [wx.Button(self.panel, label="Input", size=(150, 50), pos=(200, 640)),
wx.Button(self.panel, label="Refresh", size=(150, 50), pos=(400, 640)),
wx.Button(self.panel, label="Return", size=(150, 50), pos=(600, 640))]
self.evt_list2 = [self.Event_input, self.Event_refresh, self.Event_return]
for i, j in enumerate(self.bt_list2):
j.SetFont(self.font3)
j.SetBackgroundColour('aquamarine')
j.Bind(wx.EVT_BUTTON, self.evt_list2[i])
def Event3(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
else:
datalist = self.Readfile()
searchdialog = SearchDialog(datalist)
searchdialog.Show()
def Event4(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
else:
datalist = self.Readfile()
deletedialog = DeleteDialog(datalist)
deletedialog.Show()
def Event5(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
else:
datalist = self.Readfile()
modifydialog = ModifyDialog(datalist)
modifydialog.Show()
def Event6(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
else:
datalist = self.Readfile()
insertdialog = InsertDialog(datalist)
insertdialog.Show()
def Event7(self,event):
m=self.Checkfile()
if m==0:
wx.MessageBox("No record!")
else:
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('select * from student order by sum desc')
result = cursor.fetchall()
cursor.execute('delete from student')
cursor.executemany('insert into student(number,name,elective,experiment,required_course,sum)values(?,?,?,?,?,?)',result)
cursor.close()
conn.commit()
conn.close()
wx.MessageBox("Save successfully!")
def Event8(self,event):
m=self.Countfile()
if m==0:
wx.MessageBox("No record!")
else:
wx.MessageBox("The class are {:d} records".format(m))
def Event_input(self,event):
dlg = InputDialog(self.Refreshfunc)
dlg.ShowModal()
dlg.Destroy()
def Event_refresh(self,event):
self.Refresh_show()
def Event_return(self,event):
self.panel.Destroy()
self.panel = wx.Panel(self, size=(956, 770))
self.panel.Bind(wx.EVT_ERASE_BACKGROUND, self.onEraseBack)
self.Create_mainmenu()
def Checkfile(self):
m=0
if os.path.exists("data.db") == False:
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('create table student(number varchar(20) primary key,name varchar(20),elective double,experiment double,required_course double,sum double)')
cursor.close()
conn.close()
else:
fileinfo = os.stat("data.db")
m = fileinfo.st_size
return m
def Countfile(self):
m=0
if os.path.exists("data.db") == False:
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('create table student(number varchar(20) primary key,name varchar(20),elective double,experiment double,required_course double,sum double)')
cursor.close()
conn.close()
else:
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('select count(*) from student')
line = cursor.fetchone()
m = line[0]
return m
def Readfile(self):
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('select * from student')
datalist = cursor.fetchall()
cursor.close()
conn.close()
return datalist
def Refresh_show(self):
datalist = self.Readfile()
self.table.Clear()
for i in datalist:
for j in i:
self.table.AppendText(str(j)+"\t\t\t")
self.table.AppendText("\n\n")
def Refreshfunc(self,flag):
if flag == True:
self.Refresh_show()
class InsertDialog(wx.Dialog):
def __init__(self,datalist):
wx.Dialog.__init__(self,None,-1,title="Insert function dialog",size=(500,450))
self.InitUI()
self.datalist = datalist
def InitUI(self):
self.font3 = wx.Font(18,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.panel = wx.Panel(self)
self.data = {}
self.tc_list = [wx.StaticText(self.panel,label="Please input position",pos=(120,80)),
wx.StaticText(self.panel, label="where do you want to insert", pos=(80, 110)),
wx.StaticText(self.panel, label="(input number):", pos=(130, 140)),
wx.TextCtrl(self.panel,-1,'',(150, 200),(200, 30),wx.TE_RIGHT,MyValidator(self.data,'num1','int'))
]
self.bt_list = [wx.Button(self.panel, -1, 'Next', pos=(100,350), size=(120, 40)),
wx.Button(self.panel, wx.ID_CANCEL, 'Return', pos=(300, 350), size=(120, 40))
]
for i in self.bt_list:
i.SetFont(self.font3)
i.SetBackgroundColour('cyan')
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_next1)
for i in self.tc_list:
i.SetFont(self.font3)
def Event_next1(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
self.num1 = self.data['num1']
flag = False
for index,item in enumerate(self.datalist):
if item[0]==self.num1:
self.k = index
flag = True
break
if flag == True:
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel,label="Please input new number:",pos=(100,100)),
wx.TextCtrl(self.panel,-1,'',(150, 200),(200, 30),wx.TE_RIGHT,
MyValidator(self.data,'num2','int'))]
self.bt_list[0].Unbind(wx.EVT_BUTTON, handler=self.Event_next1)
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_next2)
for i in self.tc_list:
i.SetFont(self.font3)
else:
wx.MessageBox("Cannot find the student!")
def Event_next2(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
self.num2 = self.data['num2']
flag = False
for item in self.datalist:
if item[0]==self.num2:
flag = True
break
if flag == False:
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel, label="Please input per centum", pos=(120, 20)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 80)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 180)),
wx.StaticText(self.panel, label="Requierd course:", pos=(100, 280)),
wx.TextCtrl(self.panel, -1, '', (300, 80), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'elec_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 180), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'expe_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 280), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'requ_cen'))]
for i in self.tc_list:
i.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON, handler=self.Event_next2)
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_next3)
else:
wx.MessageBox("The number is existing,please try again!")
def Event_next3(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel, label="Please input information", pos=(100, 20)),
wx.StaticText(self.panel, label="Name:", pos=(100, 100)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 160)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 220)),
wx.StaticText(self.panel, label="Required course:", pos=(100, 280)),
wx.TextCtrl(self.panel, size=(200, 30), pos=(210, 100), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'name', 'any')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 160), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'elec')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 220), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'expe')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 280), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'requ'))
]
for i in self.tc_list:
i.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON, handler=self.Event_next3)
self.bt_list[0].SetLabel('Save')
self.bt_list[0].SetBackgroundColour('red')
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_save)
def Event_save(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
k = self.k
name = self.data['name']
summ = self.data['elec'] * self.data['elec_cen'] \
+ self.data['expe'] * self.data['expe_cen'] \
+ self.data['requ'] * self.data['requ_cen']
temp = (self.num2,name,self.data['elec'],self.data['expe'],self.data['requ'],summ)
newlist = self.datalist
newlist.insert(k+1,temp)
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('delete from student')
cursor.executemany('insert into student(number,name,elective,experiment,required_course,sum)values(?,?,?,?,?,?)',newlist)
cursor.close()
conn.commit()
conn.close()
wx.MessageBox(name + " saved!")
self.Destroy()
def Writefile(self,data,n):
file = open("data","rb+")
file.seek((n+1)*72)
for i in data:
i[0] = i[0].encode("utf-8")
i[1] = i[1].encode("utf-8")
student = struct.pack("20s15sdddd",*i)
file.write(student)
file.close()
class ModifyDialog(wx.Dialog):
def __init__(self,datalist):
wx.Dialog.__init__(self,None,-1,title="Modify function dialog",size=(500,450))
self.InitUI()
self.datalist = datalist
def InitUI(self):
self.font3 = wx.Font(18, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL, faceName="黑体")
self.panel = wx.Panel(self)
self.data = {}
self.tc_list = [wx.StaticText(self.panel, label="Please input number:", pos=(120, 100)),
wx.TextCtrl(self.panel, -1, '', (150, 200), (200, 30), wx.TE_RIGHT,
MyValidator(self.data, 'num', 'int'))]
self.bt_list = [wx.Button(self.panel, -1, 'Find', pos=(100, 350), size=(120, 40)),
wx.Button(self.panel, wx.ID_CANCEL, 'Return', pos=(300, 350), size=(120, 40))]
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_find)
for i in self.bt_list:
i.SetFont(self.font3)
i.SetBackgroundColour('cyan')
for i in self.tc_list:
i.SetFont(self.font3)
def Event_find(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
self.num = self.data['num']
flag = 0
for index,item in enumerate(self.datalist):
if item[0]==self.num:
self.k = index
flag = 1
break
if flag == 1:
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel, label="Please input per centum", pos=(120, 20)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 80)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 180)),
wx.StaticText(self.panel, label="Requierd course:", pos=(100, 280)),
wx.TextCtrl(self.panel, -1, '', (300, 80), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'elec_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 180), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'expe_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 280), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'requ_cen'))]
for i in self.tc_list:
i.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON, handler=self.Event_find)
self.bt_list[0].Bind(wx.EVT_BUTTON,self.Event_next)
self.bt_list[0].SetLabel('Next')
else:
wx.MessageBox("Cannot find the student!")
def Event_next(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel, label="Please input information", pos=(90, 20)),
wx.StaticText(self.panel, label="Name:", pos=(100, 100)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 160)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 220)),
wx.StaticText(self.panel, label="Required course:", pos=(100, 280)),
wx.TextCtrl(self.panel, size=(200, 30), pos=(210, 100), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'name', 'any')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 160), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'elec')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 220), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'expe')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(310, 280), style=wx.TE_RIGHT,
validator=MyValidator(self.data, 'requ'))
]
for i in self.tc_list:
i.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON, handler=self.Event_next)
self.bt_list[0].SetLabel('Save')
self.bt_list[0].SetBackgroundColour('red')
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_save)
def Event_save(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
name = self.data['name']
summ = self.data['elec'] * self.data['elec_cen'] \
+ self.data['expe'] * self.data['expe_cen'] \
+ self.data['requ'] * self.data['requ_cen']
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
cursor.execute('update student set name=?,elective=?,experiment=?,required_course=?,sum=? where number=?',
(name,self.data['elec'],self.data['expe'],self.data['requ'],summ,self.num))
cursor.close()
conn.commit()
conn.close()
wx.MessageBox(name + " saved!")
self.Destroy()
class DeleteDialog(wx.Dialog):
def __init__(self,datalist):
wx.Dialog.__init__(self,None,-1,title="Delete function dialog",size=(500,450))
self.InitUI()
self.datalist = datalist
def InitUI(self):
self.font3 = wx.Font(18, wx.DEFAULT, wx.FONTSTYLE_NORMAL, wx.NORMAL, faceName="黑体")
self.panel = wx.Panel(self)
self.data = {}
self.tc_list = [wx.StaticText(self.panel, label="Please input number:", pos=(120, 100)),
wx.TextCtrl(self.panel, -1, '', (150, 200), (200, 30), wx.TE_RIGHT,
MyValidator(self.data, 'num', 'int'))]
self.bt_list = [wx.Button(self.panel, -1, 'Find', pos=(100, 350), size=(120, 40)),
wx.Button(self.panel, wx.ID_CANCEL, 'Return', pos=(300, 350), size=(120, 40))]
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_next)
for i in self.bt_list:
i.SetFont(self.font3)
i.SetBackgroundColour('cyan')
for i in self.tc_list:
i.SetFont(self.font3)
def Event_next(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
self.num = self.data['num']
flag = False
for index,item in enumerate(self.datalist):
if item[0]==self.num:
self.k = index
flag = True
break
if flag == True:
for i in self.tc_list:
i.Destroy()
self.title_find = wx.StaticText(self.panel,label="Find the student,delete?",pos=(120,120))
self.title_find.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON,handler=self.Event_next)
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_delete)
self.bt_list[0].SetLabel('Delete')
self.bt_list[0].SetBackgroundColour('red')
else:
wx.MessageBox("Cannot find the student!")
def Event_delete(self,event):
conn = sqlite3.connect('data.db')# 链接到数据库
cursor = conn.cursor()
cursor.execute('delete from student where number=?',(self.num,))# 将参数传入语句进行查找
cursor.close()
conn.commit()
conn.close()
wx.MessageBox("Delete successfully!")
self.Destroy()
class SearchDialog(wx.Dialog):
def __init__(self,datalist):
wx.Dialog.__init__(self,None,-1,title="Search function dialog",size=(500,450))
self.InitUI()
self.datalist = datalist
def InitUI(self):
self.font3 = wx.Font(18,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.panel = wx.Panel(self)
self.data = {}
self.tc_list = [wx.StaticText(self.panel,label="Please input number:",pos=(120,100)),
wx.TextCtrl(self.panel,-1,'',(150, 200),(200, 30),wx.TE_RIGHT,MyValidator(self.data,'num','int'))]
self.bt_list = [wx.Button(self.panel, -1, 'Find', pos=(100,350), size=(120, 40)),
wx.Button(self.panel, wx.ID_CANCEL, 'Return', pos=(300, 350), size=(120, 40))]
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_find)
for i in self.bt_list:
i.SetFont(self.font3)
i.SetBackgroundColour('cyan')
for i in self.tc_list:
i.SetFont(self.font3)
def Event_find(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
flag = False
for j in self.datalist:
if j[0]==self.data['num']:
self.k=j
flag = True
break
if flag == True:
for i in self.tc_list:
i.Destroy()
self.bt_list[0].Destroy()
self.tc_list = [wx.StaticText(self.panel, label="Number:", pos=(90, 40)),
wx.StaticText(self.panel, label="Name:", pos=(90, 92)),
wx.StaticText(self.panel, label="Elective:", pos=(90, 144)),
wx.StaticText(self.panel, label="Experiment:", pos=(90, 196)),
wx.StaticText(self.panel, label="Required course:", pos=(90, 248)),
wx.StaticText(self.panel, label="Sum:", pos=(90, 300)),
wx.TextCtrl(self.panel,-1,self.k[0],(190, 40),(200, 30),wx.TE_RIGHT | wx.TE_READONLY),
wx.TextCtrl(self.panel,-1,str(self.k[1]),(190, 92),(200, 30),wx.TE_RIGHT | wx.TE_READONLY),
wx.TextCtrl(self.panel,-1,str(self.k[2]),(290, 144),(100, 30),wx.TE_RIGHT | wx.TE_READONLY),
wx.TextCtrl(self.panel,-1,str(self.k[3]),(290, 196),(100, 30),wx.TE_RIGHT | wx.TE_READONLY),
wx.TextCtrl(self.panel,-1,str(self.k[4]),(290, 248),(100, 30),wx.TE_RIGHT | wx.TE_READONLY),
wx.TextCtrl(self.panel,-1,str(self.k[5]),(290, 300),(100, 30),wx.TE_RIGHT | wx.TE_READONLY)
]
for i in self.tc_list:
i.SetFont(self.font3)
else:
wx.MessageBox("Cannot find the student!")
class InputDialog(wx.Dialog):
def __init__(self,func_callback):
wx.Dialog.__init__(self, None, -1, title="Input function dialog", size=(500,450))
self.InitUI() #绘制Dialog的界面
self.func_callback = func_callback
def InitUI(self):
self.data = {}
self.font3 = wx.Font(18,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
self.panel = wx.Panel(self)
self.tc_list = [wx.StaticText(self.panel,label="Please input per centum",pos=(120,20)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 80)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 180)),
wx.StaticText(self.panel, label="Requierd course:", pos=(100, 280)),
wx.TextCtrl(self.panel,-1,'',(300,80),(100,30), wx.TE_RIGHT,
MyValidator(self.data,'elec_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 180), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'expe_cen')),
wx.TextCtrl(self.panel, -1, '', (300, 280), (100, 30), wx.TE_RIGHT,
MyValidator(self.data, 'requ_cen'))]
self.bt_list = [wx.Button(self.panel, -1, 'Next',(100,350),(120, 40)),
wx.Button(self.panel, wx.ID_CANCEL, 'Return',(300, 350),(120, 40))]
for i in self.tc_list:
i.SetFont(self.font3)
for i in self.bt_list:
i.SetFont(self.font3)
i.SetBackgroundColour('cyan')
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_next)
def Event_next(self, event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
for i in self.tc_list:
i.Destroy()
self.tc_list = [wx.StaticText(self.panel,label="Please input information",pos=(90,20)),
wx.StaticText(self.panel, label="Number:", pos=(100, 80)),
wx.StaticText(self.panel, label="Name:", pos=(100, 130)),
wx.StaticText(self.panel, label="Elective:", pos=(100, 180)),
wx.StaticText(self.panel, label="Experiment:", pos=(100, 230)),
wx.StaticText(self.panel, label="Required course:", pos=(100, 280)),
wx.TextCtrl(self.panel, size=(200, 30), pos=(200, 80), style=wx.TE_RIGHT,
validator=MyValidator(self.data,'num','int')),
wx.TextCtrl(self.panel, size=(200, 30), pos=(200, 130), style=wx.TE_RIGHT,
validator=MyValidator(self.data,'name','any')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(300, 180), style=wx.TE_RIGHT,
validator=MyValidator(self.data,'elec')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(300, 230), style=wx.TE_RIGHT,
validator=MyValidator(self.data,'expe')),
wx.TextCtrl(self.panel, size=(100, 30), pos=(300, 280), style=wx.TE_RIGHT,
validator=MyValidator(self.data,'requ'))
]
for i in self.tc_list:
i.SetFont(self.font3)
self.bt_list[0].Unbind(wx.EVT_BUTTON,handler=self.Event_next)
self.bt_list[0].SetLabel('Continue')
self.bt_list[0].SetBackgroundColour('red')
self.bt_list[0].Bind(wx.EVT_BUTTON, self.Event_continue)
def Event_continue(self,event):
if self.panel.Validate() and self.panel.TransferDataFromWindow():
print(self.data)
num = self.data['num']
name = self.data['name']
datalist=self.Readfile()
flag = 0
for i in datalist:
if num == i[0]:
flag = 1
break
if flag == 1:
wx.MessageBox("The number is existing,please try again!")
else:
summ = self.data['elec']*self.data['elec_cen']\
+self.data['expe']*self.data['expe_cen']\
+self.data['requ']*self.data['requ_cen']
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('insert into student(number,name,elective,experiment,required_course,sum)values(?,?,?,?,?,?)',(
num,name,self.data['elec'],self.data['expe'],self.data['requ'],summ
))
cursor.close()
conn.commit()
conn.close()
wx.MessageBox(name + " saved!")
self.func_callback(True)
for i in range(6,11):
self.tc_list[i].Clear()
def Readfile(self):
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
cursor.execute('select * from student')
datalist = cursor.fetchall()
cursor.close()
conn.close()
return datalist
class MyValidator(wx.Validator): # 声明验证器
def __init__(self, data, key,flag='float'):
wx.Validator.__init__(self)
self.data = data
self.key = key
self.flag = flag
if self.flag == 'float' or self.flag =='int':
self.Bind(wx.EVT_CHAR, self.OnChar) # 绑定字符事件
def Clone(self):
return MyValidator(self.data, self.key,self.flag)
def Validate(self, win): # 没有验证数据
textCtrl = self.GetWindow()
if len(textCtrl.GetValue()) == 0:
wx.MessageBox("This field must contain some text!")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour('white')
textCtrl.Refresh()
return True
def TransferToWindow(self): # 对话框打开时被调用
return True
def TransferFromWindow(self): # 对话框关闭时被调用
textCtrl = self.GetWindow()
value = textCtrl.GetValue()
if self.flag == 'float':
try:
value = float(value)
except ValueError:
wx.MessageBox('Please input correct data!')
return False
self.data[self.key] = value
return True
def OnChar(self, evt): # 数据处理
keycode = evt.GetKeyCode()
if keycode <48:# 8退格13回车46小数点
if keycode == 46 and self.flag == 'int':
return
if keycode !=8 and keycode !=13 and keycode !=46:
return
elif keycode >57:
return
evt.Skip()
if __name__=="__main__":
app = wx.App()
frame = MainFrame()
frame.Show(True)
app.MainLoop()