GUI程序设计--班级信息收集系
作者:互联网
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 #创建面板 7 panel = wx.Panel(self) 8 9 #创建“保存”和“查询”按钮,并绑定事件 10 self.bt_storage = wx.Button(panel,label="保存") 11 self.bt_storage.Bind(wx.EVT_BUTTON,self.OnclickStorage) 12 self.bt_inquire = wx.Button(panel,label ='查询') 13 self.bt_inquire.Bind(wx.EVT_BUTTON,self.OnclickInquire) 14 #创建文本,左对齐 15 self.title =wx.StaticText(panel,label ="保存信息请输入用户的学号班级和姓名\n\t查询请输入姓名或学号") 16 self.label_class =wx.StaticText(panel,label ="班级:") 17 self.text_class =wx.TextCtrl(panel,style =wx.TE_LEFT) 18 self.label_user =wx.StaticText(panel,label ="姓名:") 19 self.text_user =wx.TextCtrl(panel,style =wx.TE_LEFT) 20 self.label_number = wx.StaticText(panel,label ="学号:") 21 self.text_number = wx.TextCtrl(panel,style =wx.TE_LEFT) 22 #添加容器,容器中控件横向排列 23 hsizer_class =wx.BoxSizer(wx.HORIZONTAL) 24 hsizer_class.Add(self.label_class,proportion=0,flag=wx.ALL,border=5) 25 hsizer_class.Add(self.text_class,proportion=1,flag=wx.ALL,border=5) 26 hsizer_user = wx.BoxSizer(wx.HORIZONTAL) 27 hsizer_user.Add(self.label_user,proportion=0,flag=wx.ALL,border=5) 28 hsizer_user.Add(self.text_user,proportion=1,flag =wx.ALL,border=5) 29 hsizer_number =wx.BoxSizer(wx.HORIZONTAL) 30 hsizer_number.Add(self.label_number,proportion=0,flag=wx.ALL,border=5) 31 hsizer_number.Add(self.text_number,proportion=1,flag=wx.ALL,border=5) 32 hsizer_button =wx.BoxSizer(wx.HORIZONTAL) 33 hsizer_button.Add(self.bt_storage,proportion=0,flag=wx.ALIGN_CENTER,border=5) 34 hsizer_button.Add(self.bt_inquire,proportion=0,flag=wx.ALIGN_CENTER,border=5) 35 #添加容器,容器中的控件纵向排列 36 vsizer_all = wx.BoxSizer(wx.VERTICAL) 37 vsizer_all.Add(self.title,proportion=0,flag=wx.BOTTOM |wx.TOP |wx.ALIGN_CENTER,border=15) 38 vsizer_all.Add(hsizer_class,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 39 vsizer_all.Add(hsizer_user,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 40 vsizer_all.Add(hsizer_number,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) 41 vsizer_all.Add(hsizer_button,proportion=0,flag=wx.ALIGN_CENTER |wx.TOP,border=15) 42 panel.SetSizer(vsizer_all) 43 44 def OnclickStorage(self,event): 45 "单击保存按钮" 46 #连接数据库 47 db =pymysql.connect(host='localhost',user='root',password='L897512',database="xinji_inf",charset='utf8') 48 message ="" 49 classname =self.text_class.GetValue() #获取输入的班级 50 username =self.text_user.GetValue() #获取输入的用户名 51 number =self.text_number.GetValue() #获取输入的学号 52 #使用cursor()方法创建一个游标对象 53 cursor = db.cursor() 54 #数据列表 55 data = [(classname,username,number)] 56 try: 57 #执行sql语句,插入数据 58 sql="insert into accountpassword(class, name, number) values(%s,%s,%s)" 59 cursor.executemany(sql,data) 60 #提交数据 61 db.commit() 62 except: 63 #发生错误时回滚 64 db.rollback() 65 #关闭连接 66 db.close() 67 #判断是否为空 68 if classname =="" or username =="" or number =="": 69 message ='班级或名字或密码不能为空' 70 else: 71 message ='保存成功' #用户名或密码错误 72 wx.MessageBox(message) #弹出提示框 73 74 def OnclickInquire(self,event): 75 "单击查询按钮" 76 username =self.text_user.GetValue() #获取输入的用户名 77 number =self.text_number.GetValue() #获取输入的学号 78 db =pymysql.connect(host='localhost',user='root',password='L897512',database="xinji_inf",charset='utf8') 79 #使用cursor()方法创建一个游标对象 80 cursor = db.cursor() 81 sql ="" 82 message ="" 83 if username: 84 sql ="SELECT * FROM ACCOUNTPASSWORD \ 85 WHERE NAME = %s" #找到该名字 86 try: 87 #执行sql语句 88 cursor.execute(sql,username) 89 #获取记录 90 results =cursor.fetchall() 91 if results: #若找到不为空,输出对应的信息 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 else: 98 message ="没有这个名字" 99 except: 100 print("Error: unable to fecth data") #数据错误-- 101 elif number: 102 sql ="SELECT * FROM ACCOUNTPASSWORD \ 103 WHERE NUMBER = %s" #查找学号,同上 104 try: 105 #执行sql语句 106 cursor.execute(sql,number) 107 #获取记录 108 results =cursor.fetchall() 109 if results: 110 for row in results: 111 classname = row[0] 112 username =row[1] 113 number =row[2] 114 message ="班级:{}\n姓名:{}\n学号:{}\n".format(classname,username,number) 115 else: 116 message ="没有这个学号" 117 except: 118 print("Error: unable to fecth data") 119 else: 120 message ="班级或名字或密码不能为空" 121 #关闭数据库 122 db.close() 123 wx.MessageBox(message) 124 if __name__ =='__main__': 125 app = wx.App() #初始化应用 126 frame = MyFrame(parent=None, id=-1) #实例MyFrame类,并传递参数 127 frame.Show() #显示窗口 128 app.MainLoop() #调用主循环方法 129
结果:
保存信息: 查询信息--已保存成功 查询信息--未保存
标签:班级,--,GUI,proportion,number,hsizer,Add,self,wx 来源: https://www.cnblogs.com/lwqbk/p/15668717.html