1. 程式人生 > 其它 >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;