1 import pymysql 2 import wx 3 class MyFrame(wx.Frame): 4 def __init__(self,parent,id): 5 wx.Frame.__init__(self,parent,id,'班级信息收集',size=(400,300)) 6 panel = wx.Panel(self) 7 self.bt_storage = wx.Button(panel,label="保存") 8 self.bt_storage.Bind(wx.EVT_BUTTON,self.OnclickStorage) 9 self.bt_inquire = wx.Button(panel,label ='查询') 10 self.bt_inquire.Bind(wx.EVT_BUTTON,self.OnclickInquire) 11 self.title =wx.StaticText(panel,label ="保存信息请输入用户的学号班级和姓名\n\t查询请输入姓名或学号") 12 self.label_class =wx.StaticText(panel,label ="班级:") 13 self.text_class =wx.TextCtrl(panel,style =wx.TE_LEFT) 14 self.label_user =wx.StaticText(panel,label ="姓名:") 15 self.text_user =wx.TextCtrl(panel,style =wx.TE_LEFT) 16 self.label_number = wx.StaticText(panel,label ="学号:") 17 self.text_number = wx.TextCtrl(panel,style =wx.TE_LEFT) 18 hsizer_class =wx.BoxSizer(wx.HORIZONTAL) 19 hsizer_class.Add(self.label_class,proportion=0,flag=wx.ALL,border=5) 20 hsizer_class.Add(self.text_class,proportion=1,flag=wx.ALL,border=5) 21 hsizer_user = wx.BoxSizer(wx.HORIZONTAL) 22 hsizer_user.Add(self.label_user,proportion=0,flag=wx.ALL,border=5) 23 hsizer_user.Add(self.text_user,proportion=1,flag =wx.ALL,border=5) 24 hsizer_number =wx.BoxSizer(wx.HORIZONTAL) 25 hsizer_number.Add(self.label_number,proportion=0,flag=wx.ALL,border=5) 26 hsizer_number.Add(self.text_number,proportion=1,flag=wx.ALL,border=5) 27 hsizer_button =wx.BoxSizer(wx.HORIZONTAL) 28 hsizer_button.Add(self.bt_storage,proportion=0,flag=wx.ALIGN_CENTER,border=5) 29 hsizer_button.Add(self.bt_inquire,proportion=0,flag=wx.ALIGN_CENTER,border=5) 30 vsizer_all = wx.BoxSizer(wx.VERTICAL) 31 vsizer_all.Add(self.title,proportion=0,flag=wx.BOTTOM |wx.TOP |wx.ALIGN_CENTER,border=15) 32 vsizer_all.Add(hsizer_class,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 33 vsizer_all.Add(hsizer_user,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 34 vsizer_all.Add(hsizer_number,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 35 vsizer_all.Add(hsizer_button,proportion=0,flag=wx.ALIGN_CENTER |wx.TOP,border=15) 36 panel.SetSizer(vsizer_all) 37 38 def OnclickStorage(self,event): 39 "单击保存按钮" 40 db =pymysql.connect(host='localhost',user='root',password='L3918374',database="xinji_inf",charset='utf8') 41 message ="" 42 classname =self.text_class.GetValue() 43 username =self.text_user.GetValue() 44 number =self.text_number.GetValue() 45 cursor = db.cursor() 46 if classname =="" or username =="" or number =="": 47 message ='班级或名字或密码不能为空' 48 else: 49 data = [(classname,username,number)] 50 try: 51 sql="insert into accountpassword(class, name, number) values(%s,%s,%s)" 52 cursor.executemany(sql,data) 53 db.commit() 54 message ='保存成功' 55 except: 56 db.rollback() 57 db.close() 58 wx.MessageBox(message) 59 60 def OnclickInquire(self,event): 61 "单击查询按钮" 62 username =self.text_user.GetValue() 63 number =self.text_number.GetValue() 64 db =pymysql.connect(host='localhost',user='root',password='L3918374',database="xinji_inf",charset='utf8') 65 cursor = db.cursor() 66 sql ="" 67 message ="" 68 if username !="" and number !="": 69 sql ="SELECT * FROM ACCOUNTPASSWORD \ 70 WHERE NUMBER = %s" 71 try: 72 cursor.execute(sql,number) 73 results =cursor.fetchall() 74 if results: 75 for row in results: 76 classname = row[0] 77 username_sql =row[1] 78 number_sql =row[2] 79 if username_sql == username: 80 message ="班级:{}\n姓名:{}\n学号:{}\n".format(classname,username_sql,number_sql) 81 else: 82 message ="姓名与学号不匹配" 83 except: 84 message = "没有该学号" 85 86 elif username !="" and number =="": 87 sql ="SELECT * FROM ACCOUNTPASSWORD \ 88 WHERE NAME = %s" 89 try: 90 cursor.execute(sql,username) 91 results =cursor.fetchall() 92 for row in results: 93 classname = row[0] 94 username =row[1] 95 number =row[2] 96 message ="班级:{}\n姓名:{}\n学号:{}\n".format(classname,username,number) 97 except: 98 message ="没有这个名字" 99 elif number !="" and username =="": 100 sql ="SELECT * FROM ACCOUNTPASSWORD \ 101 WHERE NUMBER = %s" 102 try: 103 cursor.execute(sql,number) 104 results =cursor.fetchall() 105 for row in results: 106 classname = row[0] 107 username =row[1] 108 number =row[2] 109 message ="班级:{}\n姓名:{}\n学号:{}\n".format(classname,username,number) 110 except: 111 message ="没有这个学号" 112 else: 113 message ="班级或名字或密码不能为空" 114 db.close() 115 wx.MessageBox(message) 116 if __name__ =='__main__': 117 app = wx.App() 118 frame = MyFrame(parent=None,id=-1) 119 frame.Show() 120 app.MainLoop()