编程语言
首页 > 编程语言> > python基于wxPython的桌面开发:登录注册

python基于wxPython的桌面开发:登录注册

作者:互联网

loginWindow.py

import wx

from loginDAO import loginDAO

dao = loginDAO()


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, size=(235, 25), pos=(100, 50), style=wx.TE_LEFT)
        self.label_pwd = wx.StaticText(panel, label="密  码", pos=(50, 90))
        self.text_password = wx.TextCtrl(panel, size=(235, 25), pos=(100, 90), style=wx.TE_PASSWORD)
        # 设置按钮
        self.bt_confirm = wx.Button(panel, label='确定', pos=(150, 130))
        self.bt_confirm.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
        self.bt_cancel = wx.Button(panel, label='取消', pos=(255, 130))
        self.bt_cancel.Bind(wx.EVT_BUTTON, self.OnclickCancel)
        self.bt_registered = wx.Button(panel, label='注册', pos=(45, 130))
        self.bt_registered.Bind(wx.EVT_BUTTON, self.OnclickResistered)

    def OnclickResistered(self, ever):
        """单机注册按钮,执行方法"""
        username = self.text_user.GetValue()
        password = self.text_password.GetValue()
        if dao.queryName(username):
            message = '用户名已存在'

        else:
            dao.add(username, password)
            message = '注册成功'

        wx.MessageBox(message)

    def OnclickSubmit(self, evet):
        """单机确定按钮,执行方法"""
        message = ""
        username = self.text_user.GetValue()
        password = self.text_password.GetValue()
        if username == "" or password == "":
            message = '用户名或密码不能为空'
        elif username == 'csh' and password == 'csh':
            message = '登录成功'
        elif dao.query(username,password):
            message = '登录成功'
        else:
            message = '用户名和密码不匹配'
        wx.MessageBox(message)

    def OnclickCancel(self, event):
        """单机取消按钮,执行方法"""
        self.text_user.SetValue("")
        self.text_password.SetValue("")


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

loginDAO.py

import pymysql


class loginDAO:
    def __init__(self):
        pass

    def add(self, name, pwd):

        if self.query(name, pwd):
            return

        # 打开数据库连接
        db = self.get_conn()
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 插入语句
        sql = "INSERT INTO user VALUES ('%s', '%s')" % (name, pwd)
        try:
            # 执行sql语句
            cursor.execute(sql)
            # 执行sql语句
            db.commit()
            print("insert ok")
        except Exception as e:
            # 发生错误时回滚
            print(e)
            print("(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((")
            db.rollback()
        # 关闭数据库连接
        db.close()

    # 查到返回真,查不到返回假
    def query(self, name, pwd):
        cursor = self.get_conn().cursor()
        sql = 'select * from user where name = "%s" and pwd = "%s"' % (name, pwd)
        rows = cursor.execute(sql)
        if rows > 0:
            return True
        else:
            return False

    # 查到返回真,查不到返回假
    def queryName(self, name):
        cursor = self.get_conn().cursor()
        sql = 'select * from user where name = "%s"' % name
        rows = cursor.execute(sql)
        if rows > 0:
            return True
        else:
            return False

    # 与数据库建立连接
    def get_conn(self):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user="root", passwd="root", db="login", charset="utf8")
        return conn

login.sql

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `name` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `pwd` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

标签:桌面开发,name,python,self,cursor,wxPython,user,password,wx
来源: https://www.cnblogs.com/cuishh/p/16246331.html