python tkinter与Mysql数据库交互实现账号登陆
程序员文章站
2022-03-03 23:20:55
本例已经实现的数据库password,数据库的表以及表结构如下:表中已经插入的信息:实现思路无非是用户完成账户密码输入并点击登录按钮后,程序先进行数据库连接,然后根据用户提供的参数,发出相应的查询语句...
本例已经实现的数据库password,数据库的表以及表结构如下:
表中已经插入的信息:
实现思路无非是用户完成账户密码输入并点击登录按钮后,程序先进行数据库连接,然后根据用户提供的参数,
发出相应的查询语句,根据返回的查询结果给出相应的响应。
代码实现
# -*- coding: utf-8 -*- """ created on tue nov 6 14:29:54 2018 description:实现tkinter的密码验证 1.与数据库验证 version: @author: hjy """ import tkinter as tk from tkinter import messagebox import sys import pymysql class loginf(): def __init__(self,master): self.master = master self.face = tk.frame(self.master,) self.face.pack() tk.label(self.face,text='账户').pack() self.t_account = tk.entry(self.face,) self.t_account.pack() tk.label(self.face,text='密码').pack() self.t_password = tk.entry(self.face,) self.t_password.pack() btn_login = tk.button(self.face,text='login',command=self.login) btn_login.pack() def login(self,): account = self.t_account.get() password = self.t_password.get() #判空操作:略 print(account,password) #数据库处理 connection = pymysql.connect(host='localhost',user='root',port=3306) try: with connection.cursor() as cursor: command1 = "use password;" command2 = "select password from passbook where account = (%s);" cursor.execute(command1) result = cursor.execute(command2,(account)) connection.close() except: sys.exit() else: if result == 0: print('no this account!') tk.messagebox.showerror('info',"account not exist!") else: print('查找结果:',result) if cursor.fetchone()[0] == password: print('login successfully!') tk.messagebox.showinfo('info',"login successfully!") #销毁登陆界面,生成登陆后界面 self.face.destroy() homef(self.master) else: print('password input error') tk.messagebox.showerror('info',"password error!") class homef(): def __init__(self,master): self.master = master self.face = tk.frame(self.master,) self.face.pack() btn_showinfo = tk.button(self.face,text='info',command=self.showinfo) btn_showinfo.pack() def showinfo(self,): pass if __name__ == '__main__': root = tk.tk() root.title('login with password') root.geometry('200x200') loginf(root) root.mainloop()
效果示例: