• Tkinter & mysql 的登录框练习


    import tkinter as tk
    from tkinter import messagebox
    import pymysql
    
    
    class SignIn(object):
        def __init__(self, root):
            self.root = root
            self.name = None
            self.password = None
    
        def the_Window(self, ):
    
            tk.Label(self.root, text=u"用户名(*):").grid(row=1, column=1, sticky='W')
            tk.Label(self.root, text=u"密码(*):").grid(row=2, column=1, sticky='W')
            self.name = tk.Entry(self.root)
            self.name.grid(row=1, column=2, sticky='W')
            self.password = tk.Entry(self.root, show="*")
            self.password.grid(row=2, column=2, sticky='W')
            tk.Button(self.root, text=u"登录", command=self.Login).grid(row=3, column=1, sticky='W')
            tk.Button(self.root, text=u"注册", command=self.Register).grid(row=3, column=2, sticky='W')
            tk.Label(self.root, text=u"(用户名只能出现字母,数字和下划线,不少于6位.)").grid(row=1, column=3, sticky='W')
            tk.Label(self.root, text=u"(密码只能出现字母,数字和下划线,不少于12位.)").grid(row=2, column=3, sticky='W')
    
        def Login(self):
            x = self.name.get()
            y = self.password.get()
    
            if len(x) == 0 or len(y) == 0:
                return
            conn = pymysql.Connect('localhost','root','123456','test',3306)
    
            sql_select = "select * from test_db"
            cur = conn.cursor()
            try:
                print(sql_select)
                cur.execute(sql_select)
                print(cur)
    
                rs = cur.fetchall()
                print(rs)
    
                for r in rs:
                    if r[0] == x and r[1] == y:
                        messagebox.showinfo(title=u"登录成功", message=u"登录成功")
                        return
                    elif r[0] == x and r[1] != y:
                        messagebox.showerror(title=u"错误信息", message=u"密码错误")
                messagebox.showerror(title=u"error", message=u"当前用户不存在")
    
            except Exception as e:
                print(e)
    
            finally:
                cur.close()
                conn.close()
    
        def Register(self):
            x = self.name.get()
            y = self.password.get()
            if len(x) == 0 or len(y) == 0:
                return
            self.name.delete(0)
            self.password.delete(0)
            # print x, y  
            conn = pymysql.Connect('localhost','root','123456','test',3306)
            sql_select = "select username from test_db"
            sql_insert = 'insert test_db(username,password) values("%s", "%s")' % (x, y)
            cur = conn.cursor()
            try:
                cur.execute(sql_select)
                rs = cur.fetchall()
                flag = 0
                for r in rs:
                    if r[0] == x:
                        flag = 1
                        messagebox.showinfo(title=u"用户名错误", message=u"当前用户名存在,请修改")
                        break
                if flag == 0:
                    cur.execute(sql_insert)
                    print(u"插入成功")
    
                conn.commit()
    
            except Exception as e:
                conn.rollback()
                print(e)
            finally:
                cur.close()
                conn.close()
    
    
    if __name__ == "__main__":
        root = tk.Tk()
        sign = SignIn(root)
        sign.the_Window()
    
        root.mainloop() 

  • 相关阅读:
    GitHub(二)之修改项目语言类型
    GitHub(一)之图片上传问题
    Git入门操作(一)
    树莓派小白教程六部曲
    关于Ajax请求的JS封装函数
    JavaScript运动_封装模板(支持链式运动、完美运动)
    NAT-地址转换技术的配置
    JAVA连接Sql-Server教程
    Kibana对数据的可视化
    浏览器渲染机制
  • 原文地址:https://www.cnblogs.com/lixy-88428977/p/9366986.html
Copyright © 2020-2023  润新知