Python tkinter for simple login Registration final effect
- Start interface
- Registered
- Landing
SOURCE login.py
# Encoding=utf-8 fromTkinterImport * fromTkinterImportMessageBox asTkmessageboxImportMysql_connect asMysImportPymysqlImportBase64#加密 The user's password is encrypted and stored in the databasedefEncryption (Str):Str=Str. Encode (encoding="Utf-8") s2=Base64.b64encode (Str)returnS2.decode ()#解密 Verify the user password after decrypting the ciphertext returned by the databasedefDecryption (Str):Str = Str. Encode (encoding="Utf-8") S1=Base64.b64decode (Str)returnS1.decode ()#处理注册defNewUser (NAME,PWD):#声明全局变量, passed to insert Globalcursor,db pwd=Encryption (PWD)#加密 ifMys.name_exist (Cursor,name):#返回正确的值说明用户名存在Tkmessagebox.showinfo (title=' Failure ', message=' user name already exists! ')return False Else: Mys.insert_data (db=Db,cursor=Cursor,name=Name,password=PWD) Tkmessagebox.showinfo (title=' Success ', message=' registered success ')#处理直接登录defOlduser (NAME,PWD):GlobalCursor# Print (name,pwd)Password=Mys.get_password (cursor,name) password=Decryption (password)#解密 Print("Password:", password)Print(PWD)ifPassword==Pwd:tkMessageBox.showinfo (title=' Success ', message=' Landing success! Welcome back '+NameElse: Tkmessagebox.showinfo (title=' Failure ', message=' Login failed ')#处理注册窗口defSignin (): win1=TopLevel () L1=Label (win1, text="Register") L1.pack ()# The side here can be assigned to the left Rtght TOP BOTTOML2=Label (win1, text="Name:") L2.pack ()# The side here can be assigned to the left Rtght TOP BOTTOMSheet_text1=Stringvar () Sheet1=Entry (Win1, textvariable=SHEET_TEXT1) Sheet1.pack () L3=Label (win1, text="Password:") L3.pack ()# The side here can be assigned to the left Rtght TOP BOTTOMSheet_text2=Stringvar () Sheet2=Entry (Win1, textvariable=SHEET_TEXT2) Sheet2.pack ()defOn_click1 (): Name=Sheet_text1.get () pwd=Sheet_text2.get ()#调用处理新用户窗口NewUser (Str(name),Str(PWD)) Button (win1, text="Press", command=ON_CLICK1). Pack ()#处理登录窗口defLogin ():# win1 = tk.winfo_toplevel (root) #焦点绑定到当前窗口, otherwise the input cannot be obtainedWin1=TopLevel () L4=Label (win1, text="Login") L4.pack ()# The side here can be assigned to the left Rtght TOP BOTTOML5=Label (win1, text="Name:") L5.pack ()# The side here can be assigned to the left Rtght TOP BOTTOMSheet_text3=Stringvar () Sheet3=Entry (Win1, textvariable=SHEET_TEXT3) Sheet3.pack () L6=Label (win1, text="Password:") L6.pack ()# The side here can be assigned to the left Rtght TOP BOTTOMSheet_text4=Stringvar () Sheet4=Entry (Win1, textvariable=SHEET_TEXT4) Sheet4.pack ()defOn_click2 (): Name=Sheet_text3.get () pwd=Sheet_text4.get () Olduser (name,pwd) Button (win1, text="Press", command=ON_CLICK2). Pack ()#退出程序defQuit1 (): Root.quit ()if __name__ == ' __main__ ':Try: DB=Pymysql.Connect("39.106.152.189","study","stfk0615","study", Use_unicode=True, CharSet=' UTF8 ') cursor=Db.cursor ()except:Print("Connect error!") root=Tk () Root.title (' User Login window ')#分别进入不同的窗口Button (Root, text="Register", command=signin). Pack () Button (root, text="Login", command=Login). Pack () Button (root, text="Exit", command=quit1). Pack () Root.mainloop ()
mysql_connect.py//Package MySQL operation
ImportPymysql# import TracebackdefName_exist (cursor,name): Exist_sql="""SELECT PASSWORDFrom INFOWHERE name= '%s'; ' ""%(Str(name))#字符串匹配替换 nameCursor.execute (exist_sql) result=Cursor.fetchall ()ifResultreturn True #表示存在, Else:return False #表示不存在defInsert_data (Db,cursor,name,password):#插入数据Insert_sql="""INSERT into INFO (Name,password) VALUES ('%s', '%s'); "" "%(Str(name),Str(password))Try: Cursor.execute (Insert_sql)#执行sqlDb.commit ()return True except Exception:Print(Exception. args)Print(' Insert error ')return FalsedefCreat_table (Cursor,name):# Create a data tableSql="""CREATE TABLE%s(NAME CHAR (a) is not NULL,PASSWORD CHAR (+)); """%(Str(name))Try: Cursor.execute (SQL)return True except:Print("Creat_table Error")return FalsedefGet_password (Cursor,name):#得到name对应的密码Sql="""SELECT PASSWORDFrom INFOWHERE name= '%s'; ' ""%(Str(name))#字符串匹配替换 name Try: Cursor.execute (SQL) result=Cursor.fetchall ()#得到所有的结果 forAinchResult#遍历结果 should actually return as long as a value Print(a[0])returna[0]except:Print("Get_error")return Falsedef Connect():Try: DB=Pymysql.Connect("39.106.152.189","study","stfk0615","study", Use_unicode=True, CharSet=' UTF8 ')#db. Set_charset (' Utf-8 ')Cursor=Db.cursor ()# cursor.execute (' SET NAMES utf8; ') # Cursor.execute (' Set CHARACTER set UTF8; ') # cursor.execute (' SET character_set_connection=utf8; ') returnCursorexcept:Print("Connect error!")return Falseif __name__==' __main__ ': DB=Pymysql.Connect("39.106.152.189","study","stfk0615","study") cursor=Db.cursor () Db.close ()
Forwarding please indicate the source, thank you.
The source code is used to realize user authentication and experiment based on B/S three layer architecture.
Python Tkinter Implementation Simple login registration based on B/s three-tier architecture, user authentication