import tkinter as tk import tkinter.messagebox as messagebox import mysql.connector as connector

创建MySQL连接

db = mysql.connector.connect( host="localhost", user="root", passwd="cry33120217", database="abc",auth_plugin='mysql_native_password')

创建登录界面

class LoginPage(tk.Frame): def init(self, master=None): super().init(master) self.master = master self.pack() self.create_widgets()

def create_widgets(self):
    self.username_label = tk.Label(self, text="用户名")
    self.username_label.pack()
    self.username_entry = tk.Entry(self)
    self.username_entry.pack()

    self.password_label = tk.Label(self, text="密码")
    self.password_label.pack()
    self.password_entry = tk.Entry(self, show="*")
    self.password_entry.pack()

    self.login_button = tk.Button(self, text="登录", command=self.login)
    self.login_button.pack()

def login(self):
    username = self.username_entry.get()
    password = self.password_entry.get()

    # 查询MySQL数据库中的用户信息
    cursor = db.cursor()
    cursor.execute(f"SELECT * FROM users WHERE username='{username}' AND password='{password}'")
    result = cursor.fetchone()

    if result:
        # 登录成功,跳转到主界面
        self.master.switch_frame(MainPage)
    else:
        messagebox.showerror("错误", "用户名或密码错误!")

创建主界面

class MainPage(tk.Frame): def init(self, master=None): super().init(master) self.master = master self.pack() self.create_widgets()

def create_widgets(self):
    self.student_id_label = tk.Label(self, text="学生ID")
    self.student_id_label.pack()
    self.student_id_entry = tk.Entry(self)
    self.student_id_entry.pack()

    self.search_button = tk.Button(self, text="查询", command=self.search)
    self.search_button.pack()

    self.result_label = tk.Label(self, text="")
    self.result_label.pack()

def search(self):
    student_id = int(self.student_id_entry.get())

    # 查询MySQL数据库中的学生信息
    cursor = db.cursor()
    cursor.execute(f"SELECT * FROM students WHERE id={student_id}")
    result = cursor.fetchone()

    if result:
        self.result_label.config(text=f"姓名:{result[1]},年龄:{result[2]}")
    else:
        self.result_label.config(text="未找到学生信息。")

创建应用程序

class Application(tk.Tk): def init(self): super().init() self.title("登录界面") self.geometry("300x200") self.resizable(False, False) self.create_widgets()

def create_widgets(self):
    self.container = tk.Frame(self)
    self.container.pack(side="top", fill="both", expand=True)
    self.container.grid_rowconfigure(0, weight=1)
    self.container.grid_columnconfigure(0, weight=1)

    self.frames = {}

    # 建立所有的页面并保存到字典中
    for F in (LoginPage, MainPage):
        frame = F(self.container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.switch_frame(LoginPage)

def switch_frame(self, frame_class):
    frame = self.frames[frame_class]
    frame.tkraise()

运行应用程序

if name == "main": app = Application() app.mainloop()

关闭MySQL连接

db.close(

import tkinter as tkimport tkintermessagebox as messageboximport mysqlconnector as connector# 创建MySQL连接db = mysqlconnectorconnect host=localhost user=root passwd=cry33120217 database=abcau

原文地址: http://www.cveoy.top/t/topic/fJpG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录