python tkinter与Mysql数据库交互实现账号登陆
作者:IronSimon 时间:2024-01-21 19:02:39
本例已经实现的数据库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()
效果示例:
来源:https://blog.csdn.net/yeshankuangrenaaaaa/article/details/85704134
标签:python,tkinter,Mysql,账号登陆
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript实现点击出现子菜单效果
2024-04-19 10:45:56
![](https://img.aspxhome.com/file/2023/6/135776_0s.jpg)
Python实现npy/mat文件的保存与读取
2023-07-22 08:49:48
![](https://img.aspxhome.com/file/2023/8/83738_0s.png)
python实现对变位词的判断方法
2022-01-29 04:36:10
基于Python实现五子棋游戏
2023-01-29 21:47:42
![](https://img.aspxhome.com/file/2023/1/65911_0s.jpg)
python中defaultdict字典功能特性介绍
2022-06-07 23:27:45
![](https://img.aspxhome.com/file/2023/7/99037_0s.png)
css+js实现部分区域高亮可编辑遮罩层
2024-02-25 08:49:23
![](https://img.aspxhome.com/file/2023/4/56474_0s.gif)
Python3.6基于正则实现的计算器示例【无优化简单注释版】
2023-07-19 05:29:25
![](https://img.aspxhome.com/file/2023/3/79603_0s.png)
用python自动生成日历
2022-08-28 14:28:58
![](https://img.aspxhome.com/file/2023/4/72174_0s.png)
Python Django 母版和继承解析
2023-02-21 09:44:59
![](https://img.aspxhome.com/file/2023/6/76016_0s.png)
Python Tensor FLow简单使用方法实例详解
2022-01-01 16:55:44
![](https://img.aspxhome.com/file/2023/2/109562_0s.png)
django+xadmin+djcelery实现后台管理定时任务
2023-12-15 03:05:52
![](https://img.aspxhome.com/file/2023/5/97325_0s.png)
PHP操作数组的一些函数整理介绍
2023-11-24 14:24:17
Python常见字符串操作函数小结【split()、join()、strip()】
2023-07-21 04:58:15
对PyQt5中树结构的实现方法详解
2021-02-07 16:19:20
![](https://img.aspxhome.com/file/2023/3/105283_0s.jpg)
Python机器学习k-近邻算法(K Nearest Neighbor)实例详解
2023-07-12 10:50:17
![](https://img.aspxhome.com/file/2023/7/133337_0s.png)
Python入门学习之字符串与比较运算符
2023-10-14 22:09:40
![](https://img.aspxhome.com/file/2023/0/75290_0s.jpg)
使用BULK INSERT大批量导入数据 SQLSERVER
2012-01-05 19:26:36
python实现的DES加密算法和3DES加密算法实例
2023-04-28 07:44:01
详解yii2使用多个数据库的案例
2024-05-13 09:53:07
Python的matplotlib绘图如何修改背景颜色的实现
2023-10-05 08:12:43
![](https://img.aspxhome.com/file/2023/0/62900_0s.png)