Python做出用户管理系统,连接MySql,图形化界面
- 导入包
import pymysql from tkinter import ttk import tkinter as tk import tkinter.font as tkFont from tkinter import * # 图形界面库 import tkinter.messagebox as messagebox import time
- 主界面
class StartPage: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('信息管理系统') self.window.geometry('700x600+70+50') def getTime(): timeStr = time.strftime('%H:%M:%S') Rtime.configure(text=timeStr) self.window.after(1000, getTime) Rtime = Label(self.window, text='') Rtime.pack(pady=25) getTime() label = Label(self.window, text="信息管理系统", font=("楷体", 40)) label.pack(pady=100) # pady=100 界面的长度 #按钮 Button(self.window,text="管理员登陆",font=tkFont.Font(size=16),command=lambda: AdminPage(self.window),width=20, height=2,fg='white', bg='gray').place(x=100,y=300) Button(self.window, text="用户登陆", font=tkFont.Font(size=16),command=lambda: StudentPage(self.window),width=20, height=2, fg='white', bg='gray').place(x=400,y=300) Button(self.window, text="作者信息", font=tkFont.Font(size=16), command=lambda: AboutPage(self.window), width=20, height=2,fg='white', bg='gray').place(x=100,y=400) Button(self.window, text='退出系统',font=tkFont.Font(size=16),command=self.window.destroy,width=20,height=2, fg='white', bg='gray').place(x=400,y=400) self.window.mainloop() # 主消息循环
- 管理员登陆界面
class AdminPage: def __init__(self, parent_window): parent_window.destroy() # 销毁主界面 self.window = tk.Tk() # 初始框的声明 self.window.title('管理员登陆页面') self.window.geometry('700x600+70+50') # 这里的乘是小x # 菜单 menubar = Menu(self.window) menubar.add_cascade(label="管理员注册",command=lambda: Admin(self.window)) self.window.config(menu=menubar) self.window['menu'] = menubar self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击 label = tk.Label(self.window, text='管理员登陆', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack() Label(self.window, text='管理员账号:', font=tkFont.Font(size=14)).pack(pady=25) self.guanli_username = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory') self.guanli_username.pack() Label(self.window, text='管理员密码:', font=tkFont.Font(size=14)).pack(pady=25) self.guanli_pass = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory', show='*') self.guanli_pass.pack() Button(self.window, text="登陆", width=8, font=tkFont.Font(size=12), command=self.login).pack(pady=40) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back).pack() def login(self): guanli_pass = None # 数据库操作 查询管理员表 db = pymysql.connect("localhost", "root", "lmxzjddf1122", "182") # 打开数据库连接 cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM guanli WHERE guanli_id = '%s'" % (self.guanli_username.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: guanli_id = row[0] guanli_pass = row[1] # 打印结果 except: print("Error: unable to fecth data") messagebox.showinfo('警!', '用户名或密码不正确!') db.close() # 关闭数据库连接 if self.guanli_pass.get() == guanli_pass: AdminManage(self.window) # 进入管理员操作界面 else: messagebox.showinfo('警告!', '用户名或密码不正确!') def back(self): StartPage(self.window) # 显示主窗口 销毁本窗口
- 管理员注册界面
class Admin: def __init__(self, parent_window): parent_window.destroy() # 销毁主界面 self.window = tk.Tk() # 初始框的声明 self.window.title('管理员注册页面') self.window.geometry('700x600+70+50') # 这里的乘是小x label = tk.Label(self.window, text='管理员注册', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack() Label(self.window, text='输入你的编号:', font=tkFont.Font(size=14)).pack(pady=25) self.teacher_id = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory') self.teacher_id.pack() Label(self.window, text='注册账号:', font=tkFont.Font(size=14)).pack(pady=25) self.guanli_username = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory') self.guanli_username.pack() Label(self.window, text='注册密码:', font=tkFont.Font(size=14)).pack(pady=25) self.guanli_pass = tk.Entry(self.window, width=30, font=tkFont.Font(size=14), bg='Ivory') self.guanli_pass.pack() Button(self.window, text="确定", width=8, font=tkFont.Font(size=12), command=self.login).pack(pady=40) Button(self.window, text="返回", width=8, font=tkFont.Font(size=12), command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击 def login(self): id = None # 数据库操作 查询管理员表 db = pymysql.connect("localhost", "root", "lmxzjddf1122", "182") # 打开数据库连接 cursor = db.cursor() # 使用cursor()方法获取操作游标 sq = "SELECT * FROM teacher WHERE id = '%s'" % (self.teacher_id.get()) # SQL 查询语句 try: # 执行SQL语句 # 获取所有记录列表 cursor.execute(sq) results = cursor.fetchall() for row in results: id = row[0] name = row[1] except: print("Error: unable to fecth data") messagebox.showinfo('警告!', '失败') db.close() # 关闭数据库连接 if self.teacher_id.get() != id: messagebox.showinfo('警告!', '你不在名册内!') else: if self.guanli_username.get() != '' and self.guanli_pass.get() != '': db = pymysql.connect("localhost", "root", "lmxzjddf1122", "182") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "INSERT INTO guanl