GUI程序设计--班级信息收集系
import pymysql
import wx
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,title="学生信息收集",size=(400,300))
#创建面板
panel=wx.Panel(self)
#创建文本和密码输入框
self.title =wx.StaticText(panel ,label="请输入姓名和学号",pos=(140,20))
self.label_user =wx.StaticText(panel,label="姓 名:",pos=(50,50))
self.text_user =wx.TextCtrl(panel,pos=(100,50),size=(235,25),style=wx.TE_LEFT)
self.label_number =wx.StaticText(panel,pos=(50,90),label="学 号:")
self.text_number =wx.TextCtrl(panel,pos=(100,90),size=(235,25),style=wx.TE_LEFT)
# 创建“确定”和“取消”按钮及添加事件
self.bt_confirm =wx.Button(panel,label='添加',pos=(105,130))
self.bt_confirm.Bind(wx.EVT_BUTTON,self.OnclickAdd)
self.bt_cancel =wx.Button(panel,label='查询',pos=(195,130))
self.bt_cancel.Bind(wx.EVT_BUTTON,self.OnclickCheck)
self.message=''
def OnclickCheck(self,event):
username = self.text_user.GetValue()
usernumber = self.text_number.GetValue()
#打开数据库
db = pymysql.connect(host="localhost",user="root",password="密码",database='表'
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = '''select * from 表 '''
cursor.execute(sql)
a = cursor.fetchall()
if username == "" or usernumber == "":
self.message = '姓名或学号不能为空'
else:
# 执行sql语句
# cursor.execute(sql,usernumber)
# 获取所有命令行
results = cursor.fetchall()
for row in a:
sql_name = row[0]
sql_number = row[1]
if username == sql_name and usernumber == sql_number:
self.message = '学生信息正确'
# wx.MessageBox(message)
break
else:
self.message = '学号姓名不匹配'
break
wx.MessageBox(self.message)
# 关闭数据库连接
db.close()
def OnclickAdd(self,event):
message = ""
user_name = self.text_user.GetValue()
user_number = self.text_number.GetValue()
db = pymysql.connect(host="localhost",user="root",password="密码",database='表')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
data = [(user_name,user_number)]
# SQL 插入语句
sql = "INSERT INTO 表(name, \
number) \
VALUES (%s, %s)"
if user_name == "" or user_number == "":
message = '学号和姓名不能为空'
elif len(user_name) >=1 and len(user_number) == 13:
try:
# 执行sql语句
cursor.executemany(sql,data)
# 提交到数据库执行
db.commit()
message = '学生添加成功'
except:
# 关闭数据库连接
db.close()
else:
message = '填写格式不对'
wx.MessageBox(message)
app =wx.App() # 初始化
frame =MyFrame(parent=None,id=-1) #实例MyFrame类,并传递参数
frame.Show() #显示窗口
app.MainLoop() # 调用主循环方法 import wx