import pymysql
import wx
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"班级信息收集",size=(430,300))
panel=wx.Panel(self)
self.bt_storage=wx.Button(panel,label='保存',pos=(120,170))
self.bt_storage.Bind(wx.EVT_BUTTON,self.OnclickStorage)
self.bt_inquire=wx.Button(panel,label='查询',pos=(215,170))
self.bt_inquire.Bind(wx.EVT_BUTTON,self.OnclickInquire)
self.title=wx.StaticText(panel,label="请输入以下信息进行保存或查询",pos=(130,20))
self.label_class=wx.StaticText(panel,label='班级:',pos=(50,50))
self.text_class=wx.TextCtrl(panel,pos=(100,50),size=(235,25),style=wx.TE_LEFT)
self.label_name=wx.StaticText(panel,label='姓名:',pos=(50,90))
self.text_name=wx.TextCtrl(panel,pos=(100,90),size=(235,25),style=wx.TE_LEFT)
self.label_number=wx.StaticText(panel,label='学号:',pos=(50,130))
self.text_number=wx.TextCtrl(panel,pos=(100,130),size=(235,25),style=wx.TE_LEFT)
def OnclickStorage(self,event):
"""单击保存按钮,执行方法"""
db=pymysql.connect(host="localhost",user="root",password="ruirui20020908",database="banji",charset='utf8')
message=""
classname=self.text_class.GetValue()
name=self.text_name.GetValue()
number=self.text_number.GetValue()
cursor=db.cursor()
data=[classname,name,number]
if data[0]=='' or data[1]=='' or data[2]=='':
message="班级或姓名或学号不能为空"
else:
try:
cursor.executemany("insert into xinji(class,name,number) values(%s,%s,%s)",data)
db.commit()
except:
db.rollback()
db.close()
message="保存成功"
wx.MessageBox(message)
def OnclickInquire(self,event):
"""单击查询按钮,执行方法"""
classname_1=self.text_class.GetValue()
name_1=self.text_name.GetValue()
number_1=self.text_number.GetValue()
data=[classname_1,name_1,number_1]
db=pymysql.connect(host="localhost",user="root",password="ruirui20020908",database="banji")
cursor=db.cursor()
if name_1!='' and classname_1!='':
try:
cursor.execute('select * from xinji where name=%s')
result=cursor.fetchall()
if result:
classname=result[0]
name=result[1]
number=result[2]
if number==number_1:
message="班级:{}\n姓名:{}\n学号:{}\n".format(classname,name,number)
else:
message="没有该学号"
except:
db.rollback()
db.close()
wx.MessageBox(message)
if __name__ == '__main__':
app=wx.App()
frame=MyFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()