基于tkinter+mysql登录界面系统
时间: 2023-07-30 15:10:25 浏览: 43
基于tkinter+mysql登录界面系统的开发可以分为以下几步:
1. 安装MySQL数据库和Python的MySQL驱动程序。
2. 创建一个MySQL数据库和一张用户表,用于存储用户信息。
3. 使用Python连接MySQL数据库,并编写SQL语句实现用户信息的查询、插入、更新和删除等操作。
4. 使用Tkinter创建登录窗口,包括输入用户名和密码的文本框、登录按钮和注册按钮等控件。
5. 在登录按钮的事件处理函数中,获取用户名和密码,使用Python查询用户信息,判断用户是否存在并且密码是否正确。如果验证通过,则跳转到主界面,否则提示用户登录失败。
6. 在注册按钮的事件处理函数中,弹出一个注册窗口,包括输入用户名、密码和确认密码的文本框、注册按钮和取消按钮等控件。在注册按钮的事件处理函数中,获取用户输入的信息,使用Python插入新的用户信息到数据库中。
下面是一个基于tkinter+mysql登录界面系统的代码示例:
```python
import tkinter as tk
import tkinter.messagebox
import pymysql
class LoginWindow:
def __init__(self):
self.win = tk.Tk()
self.win.title('登录')
self.win.geometry('300x200')
tk.Label(self.win, text='用户名:').grid(row=0, column=0)
self.username_entry = tk.Entry(self.win)
self.username_entry.grid(row=0, column=1)
tk.Label(self.win, text='密码:').grid(row=1, column=0)
self.password_entry = tk.Entry(self.win, show='*')
self.password_entry.grid(row=1, column=1)
tk.Button(self.win, text='登录', command=self.login).grid(row=2, column=0)
tk.Button(self.win, text='注册', command=self.register).grid(row=2, column=1)
self.conn = pymysql.connect(host='localhost', user='root', password='root', database='test', charset='utf8')
self.cursor = self.conn.cursor()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
sql = f"SELECT * FROM user WHERE username='{username}' AND password='{password}'"
self.cursor.execute(sql)
result = self.cursor.fetchone()
if result:
tk.messagebox.showinfo('登录成功', f'欢迎回来,{username}!')
self.win.destroy()
MainWindow()
else:
tk.messagebox.showerror('登录失败', '用户名或密码错误!')
def register(self):
RegisterWindow(self.cursor)
def __del__(self):
self.cursor.close()
self.conn.close()
class RegisterWindow:
def __init__(self, cursor):
self.win = tk.Toplevel()
self.win.title('注册')
self.win.geometry('300x200')
tk.Label(self.win, text='用户名:').grid(row=0, column=0)
self.username_entry = tk.Entry(self.win)
self.username_entry.grid(row=0, column=1)
tk.Label(self.win, text='密码:').grid(row=1, column=0)
self.password_entry = tk.Entry(self.win, show='*')
self.password_entry.grid(row=1, column=1)
tk.Label(self.win, text='确认密码:').grid(row=2, column=0)
self.confirm_password_entry = tk.Entry(self.win, show='*')
self.confirm_password_entry.grid(row=2, column=1)
tk.Button(self.win, text='注册', command=self.register).grid(row=3, column=0)
tk.Button(self.win, text='取消', command=self.win.destroy).grid(row=3, column=1)
self.cursor = cursor
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
confirm_password = self.confirm_password_entry.get()
if password != confirm_password:
tk.messagebox.showerror('注册失败', '两次输入的密码不一致!')
return
sql = f"SELECT * FROM user WHERE username='{username}'"
self.cursor.execute(sql)
result = self.cursor.fetchone()
if result:
tk.messagebox.showerror('注册失败', '用户名已存在!')
else:
sql = f"INSERT INTO user (username, password) VALUES ('{username}', '{password}')"
self.cursor.execute(sql)
tk.messagebox.showinfo('注册成功', '注册成功!')
self.win.destroy()
class MainWindow:
def __init__(self):
self.win = tk.Tk()
self.win.title('主界面')
self.win.geometry('300x200')
tk.Label(self.win, text='欢迎使用本系统!').pack()
if __name__ == '__main__':
LoginWindow()
```
这个程序中,LoginWindow类表示登录窗口,RegisterWindow类表示注册窗口,MainWindow类表示主界面。程序使用pymysql模块连接MySQL数据库,使用tkinter模块创建GUI界面。在登录按钮的事件处理函数中,通过SQL语句查询用户信息,判断用户是否存在并且密码是否正确。在注册按钮的事件处理函数中,弹出一个注册窗口,等待用户输入用户名和密码,并将新的用户信息插入到数据库中。如果登录成功,则跳转到主界面。