Tkinter & mysql的登录框设计

    xiaoxiao2021-12-15  25

    #!/usr/bin/python #coding: utf-8 from Tkinter import * from tkMessageBox import * import pymysql class SignIn(object): def __init__(self, root): self.root = root self.name = None self.password = None def the_Window(self,): Label(self.root, text = u"用户名(*):").grid(row = 1, column = 1, sticky = W) Label(self.root, text = u"密码(*):").grid(row = 2, column = 1, sticky = W) self.name = Entry(self.root) self.name.grid(row = 1, column = 2, sticky = W) self.password = Entry(self.root, show = "*") self.password.grid(row = 2, column = 2, sticky = W) Button(self.root, text = u"登录", command = self.Login).grid(row = 3, column = 1, sticky = W) Button(self.root, text = u"注册", command = self.Register).grid(row = 3, column = 2, sticky = W) Label(self.root, text=u"(用户名只能出现字母,数字和下划线,不少于6位.)").grid(row = 1, column = 3, sticky = W) 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(host='127.0.0.1', user='root', passwd='root', port=3306, db='test' ) 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: showinfo(title=u"登录成功", message=u"登录成功") return elif r[0] == x and r[1] != y: showerror(title= u"错误信息", message= u"密码错误") 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, END) self.password.delete(0, END) # print x, y conn = pymysql.connect(host='127.0.0.1', user='root', passwd='root', port=3306, db='test' ) 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 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() sign = SignIn(root) sign.the_Window() root.mainloop() 运行出来的界面很丑

    转载请注明原文地址: https://ju.6miu.com/read-1000165.html

    最新回复(0)