import wx
import pymysql
#主要窗口类
class LoginFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'用户登录及注册',pos = (700,400),size=(400,250))
panel = wx.Panel(self)
self.bt_confirm = wx.Button(panel,label = '查询')
self.bt_confirm.Bind(wx.EVT_BUTTON,self.selectController)
self.bt_cancel = wx.Button(panel,label = '注册')
self.bt_cancel.Bind(wx.EVT_BUTTON,self.registerController)
self.title = wx.StaticText(panel, label = '支持输入姓名查询学号,没注册的输入名字以及学号将自动注册')
self.label_user = wx.StaticText(panel, label = '姓名')
self.text_user = wx.TextCtrl(panel, style = wx.TE_LEFT)
self.label_userid = wx.StaticText(panel, label = '学号')
self.text_userid = wx.TextCtrl(panel, style = wx.TE_LEFT)
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user, proportion = 0,flag = wx.ALL,border = 5)
hsizer_user.Add(self.text_user, proportion = 1,flag = wx.ALL,border = 5)
hsizer_userid = wx.BoxSizer(wx.HORIZONTAL)
hsizer_userid.Add(self.label_userid, proportion = 0,flag = wx.ALL,border = 5)
hsizer_userid.Add(self.text_userid, proportion = 1,flag = wx.ALL,border = 5)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion = 0,flag = wx.ALL,border = 5)
hsizer_button.Add(self.bt_cancel, proportion = 0,flag = wx.ALL,border = 5)
viszer_all = wx.BoxSizer(wx.VERTICAL)
viszer_all.Add(self.title, proportion = 0, flag = wx.BOTTOM|wx.TOP|wx.ALIGN_CENTER, border = 15)
viszer_all.Add(hsizer_user, proportion = 0, flag = wx.EXPAND|wx.LEFT|wx.RIGHT, border = 45)
viszer_all.Add(hsizer_userid, proportion = 0, flag = wx.EXPAND|wx.LEFT|wx.RIGHT, border = 45)
viszer_all.Add(hsizer_button, proportion = 0, flag = wx.ALIGN_CENTER|wx.TOP, border = 15)
panel.SetSizer(viszer_all)
'''
没将判断逻辑做好
查询要是不存在在数据库中那直接就不能在后面继续使用,没找到解决办法
'''
#控制查询方法
def selectController(self, event):
username = self.text_user.GetValue()
result = selectOne(username)
wx.MessageBox(username+'的学号为:'+result[0])
#控制写入方法
def registerController(self, event):
insertOne(self.text_userid.GetValue(),self.text_user.GetValue())
wx.MessageBox('添加成功')
#链接数据库并查询语句
def selectOne(username):
conn = pymysql.connect(host='localhost',user='root',password='1002466752asdfgHJKL',database='studentinformation')
cursor = conn.cursor()
cursor.execute("select * from information where username = %s",(username,))
result = cursor.fetchone()
cursor.close()
conn.close()
return result
#链接数据库并输入语句
def insertOne(userid,username):
conn = pymysql.connect(host='localhost',user='root',password='1002466752asdfgHJKL',database='studentinformation')
cursor = conn.cursor()
cursor.execute("insert into information (userid,username) values (%s,%s)",(userid,username))
cursor.close()
conn.commit()
conn.close()
if __name__ == '__main__':
app = wx.App()
frame = LoginFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()