自学内容网 自学内容网

社团信息管理系统

代码语言: 

python

第三方库:

mysql

tkinter

PIL

datetime

这是一个demo,用于数据库练手,逻辑及功能不全,仍需要补充

import mysql.connector
import tkinter as tk
from tkinter import messagebox
from tkinter.ttk import Treeview, Scrollbar
from PIL import Image, ImageTk
from tkinter import IntVar
from datetime import datetime

p_id = 2


def CreateDatabase():
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******"
    )

    cursor = db.cursor()
    cursor.execute("CREATE DATABASE IF NOT EXISTS infsys")
    cursor.execute("USE infsys")
    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS 社团信息汇总(
        name varchar(255) comment '社团名称',
        num int comment '社团人数',
        type varchar(255) comment '社团类型',
        time datetime comment '社团成立时间',
        chairman varchar(255) comment '社团负责人',
        p_id int comment '对应的表'
        )
        """
    )

    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS 文学社(
        name varchar(255) comment '成员姓名',
        posts varchar(255) comment '职位',
        gender varchar(1) comment '性别',
        age int comment '年龄',
        academy varchar(255) comment '学院',
        phone varchar(255) comment '电话'
        )
        """
    )

    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS 体育社(
        name varchar(255) comment '成员姓名',
        posts varchar(255) comment '职位',
        gender varchar(1) comment '性别',
        age int comment '年龄',
        academy varchar(255) comment '学院',
        phone varchar(255) comment '电话'
        )
        """
    )

    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS 管理员信息(
        name varchar(255) comment '管理员姓名',
        id varchar(255) comment '学号',
        password varchar(255) comment '密码'
        )   
        """
    )

    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS 社长信息(
        name varchar(255) comment '社长姓名',
        id varchar(255) comment '学号',
        password varchar(255) comment '密码'
        )
        """
    )

    cursor.close()
    db.close()


def InsertData():
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    # 向社团信息汇总表中插入数据
    cursor.execute(
        """
        INSERT INTO 社团信息汇总 (name, num, type, time, chairman, p_id)
        VALUES ('文学社', 50, '文学', '2022-01-01 00:00:00', '张三', 1),
               ('体育社', 60, '体育', '2021-01-01 00:00:00', '李四', 2)
        """
    )

    # 向文学社表中插入数据
    cursor.execute(
        """
        INSERT INTO 文学社 (name, posts, gender, age, academy, phone)
        VALUES ('张三', '社长', 'M', 22, '文学院', '1234567890'),
               ('李四', '副社长', 'F', 21, '文学院', '0987654321')
        """
    )

    # 向体育社表中插入数据
    cursor.execute(
        """
        INSERT INTO 体育社 (name, posts, gender, age, academy, phone)
        VALUES ('王五', '社长', 'M', 23, '体育学院', '1231231234'),
               ('赵六', '副社长', 'F', 22, '体育学院', '4321432143')
        """
    )

    cursor.execute(
        """
        INSERT INTO 管理员信息 (name, id, password)
        VALUES ('HNU', '20222022', '123456')
        """
    )

    cursor.execute(
        """
        INSERT INTO 社长信息 (name, id ,password)
        VALUES ('张三', '20221111', '123456'),
               ('李四', '20221112', '123456'),
               ('王五', '20221113', '123456'),
               ('赵六', '20221114', '123456')    
        """
    )
    db.commit()  # 提交事务

    cursor.close()
    db.close()


def login():
    username = entry_username.get()
    password = entry_password.get()
    role = role_var.get()
    # print(username, password, role)

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    if role == 1:
        cursor.execute(f"SELECT * FROM 管理员信息 WHERE name='{username}' AND password='{password}'")
        result = cursor.fetchone()

        if result:
            messagebox.showinfo("登录成功", "管理员登录成功!")
            root.destroy()
            admin_panel()
        else:
            messagebox.showerror("登录失败", "密码错误!")

    elif role == 2:
        cursor.execute(f"SELECT * FROM 社长信息 WHERE name='{username}' AND password='{password}'")
        result = cursor.fetchone()

        if result:
            messagebox.showinfo("登录成功", "社长登录成功!")
            root.destroy()
            president_panel(username)
        else:
            messagebox.showerror("登录失败", "密码错误!")

    cursor.close()
    db.close()


def show_information(key):
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()
    cursor.execute("SELECT * FROM {}".format(key))

    results = cursor.fetchall()

    cursor.close()
    db.close()

    return results


def submit_club_information(entry_name, entry_num, entry_type, entry_chairman, entry_id, entry_password):
    global p_id

    club_name = entry_name.get()
    num = entry_num.get()
    clubtype = entry_type.get()
    chairman = entry_chairman.get()
    time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    id = entry_id.get()
    password = entry_password.get()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    p_id += 1

    cursor = db.cursor()
    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS club_name(
        name varchar(255) comment '成员姓名',
        posts varchar(255) comment '职位',
        gender varchar(1) comment '性别',
        age int comment '年龄',
        academy varchar(255) comment '学院',
        phone varchar(255) comment '电话'
        )
        """
    )
    cursor.execute(
        """
        INSERT INTO 社团信息汇总 (name, num, type, time, chairman, p_id)
        VALUES (%s, %s, %s, %s, %s, %s)
        """, (club_name, num, clubtype, time, chairman, p_id)
    )
    cursor.execute(
        """
        INSERT INTO 社长信息 (name, id, password)
        VALUES (%s, %s, %s)
        """, (chairman, id, password)
    )
    db.commit()
    cursor.close()
    db.close()

    new_window.destroy()
    messagebox.showinfo("成功", "社团数据添加成功")

    admin.destroy()
    admin_panel()  # 重新加载管理员面板以刷新数据


def new_club_information():
    global new_window, p_id

    new_window = tk.Toplevel(master=admin)
    new_window.title("添加社团数据")
    new_window.geometry("500x500+500+200")
    new_window.resizable(False, False)

    new_club_image = Image.open('秋.jpg')
    new_club_image = new_club_image.resize((500, 500))
    new_club_photo = ImageTk.PhotoImage(new_club_image)

    new_club_background_label = tk.Label(new_window, image=new_club_photo)
    new_club_background_label.place(x=0, y=0)

    tk.Label(new_window, text="社团名称:").place(x=50, y=50)
    tk.Label(new_window, text="社团人数:").place(x=50, y=100)
    tk.Label(new_window, text="社团类型:").place(x=50, y=150)
    tk.Label(new_window, text="社长姓名:").place(x=50, y=200)
    tk.Label(new_window, text="社长学号:").place(x=50, y=250)
    tk.Label(new_window, text="密码:").place(x=50, y=300)

    entry_name = tk.Entry(new_window, width=20)
    entry_name.place(x=150, y=50)
    entry_num = tk.Entry(new_window, width=20)
    entry_num.place(x=150, y=100)
    entry_type = tk.Entry(new_window, width=20)
    entry_type.place(x=150, y=150)
    entry_chairman = tk.Entry(new_window, width=20)
    entry_chairman.place(x=150, y=200)
    entry_id = tk.Entry(new_window, width=20)
    entry_id.place(x=150, y=250)
    entry_password = tk.Entry(new_window, show='*', width=20)
    entry_password.place(x=150, y=300)

    tk.Button(new_window, text="提交",
              command=lambda: submit_club_information(entry_name, entry_num, entry_type,
                                                      entry_chairman, entry_id, entry_password)).place(x=150, y=350)


def delete_club_information(entry_name, entry_chairman):
    club_name = entry_name.get()
    chairman = entry_chairman.get()
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    try:
        # 删除社团表
        cursor.execute(f"DROP TABLE IF EXISTS {club_name}")

        # 删除社团信息汇总中的记录
        cursor.execute("DELETE FROM 社团信息汇总 WHERE name = %s", (club_name,))

        cursor.execute("DELETE FROM 社长信息 WHERE name = %s", (chairman,))

        db.commit()
        messagebox.showinfo("成功", "社团数据删除成功")
    except mysql.connector.Error as err:
        messagebox.showerror("错误", f"删除社团数据时发生错误: {err}")

    cursor.close()
    db.close()

    delete_window.destroy()
    admin.destroy()
    admin_panel()  # 重新加载管理员面板以刷新数据


def remove_club_information():
    global delete_window

    delete_window = tk.Toplevel(master=admin)
    delete_window.title("删除社团数据")
    delete_window.geometry("500x500+500+200")

    delete_club_image = Image.open('秋.jpg')
    delete_club_image = delete_club_image.resize((500, 500))
    delete_club_photo = ImageTk.PhotoImage(delete_club_image)

    delete_club_background_label = tk.Label(delete_window, image=delete_club_photo)
    delete_club_background_label.place(x=0, y=0)

    tk.Label(delete_window, text="社团名称:").place(x=50, y=50)
    tk.Label(delete_window, text="社长姓名:").place(x=50, y=100)

    entry_name = tk.Entry(delete_window, width=20)
    entry_name.place(x=150, y=50)
    entry_chairman = tk.Entry(delete_window, width=20)
    entry_chairman.place(x=150, y=100)

    tk.Button(delete_window, text="提交",
              command=lambda: delete_club_information(entry_name, entry_chairman)).place(x=150, y=150)


def search_club_information(entry_name, entry_chairman):
    club_name = entry_name.get()
    chair_name = entry_chairman.get()
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    try:
        cursor.execute("SELECT * FROM 社团信息汇总 WHERE name = %s", (club_name,))
        club_info = cursor.fetchone()
        cursor.execute("SELECT * FROM 社长信息 WHERE name = %s", (chair_name,))
        chair_info = cursor.fetchone()

        if club_info:
            info_text = f"社团名称: {club_info[0]}\n社团人数: {club_info[1]}\n社团类型: {club_info[2]}\n成立时间: {club_info[3]}\n负责人: {club_info[4]}\n学号: {chair_info[1]}\n密码: {chair_info[2]}"
            messagebox.showinfo("社团信息", info_text)
        else:
            messagebox.showinfo("无结果", "未找到相关社团信息")
    except mysql.connector.Error as err:
        messagebox.showerror("错误", f"查询社团信息时发生错误: {err}")

    cursor.close()
    db.close()


def query_club_information():
    global query_window

    query_window = tk.Toplevel(master=admin)
    query_window.title("查询社团信息")
    query_window.geometry("500x500+500+200")

    query_club_image = Image.open('秋.jpg')
    query_club_image = query_club_image.resize((500, 500))
    query_club_photo = ImageTk.PhotoImage(query_club_image)

    query_club_background_label = tk.Label(query_window, image=query_club_photo)
    query_club_background_label.place(x=0, y=0)

    tk.Label(query_window, text="社团名称:").place(x=50, y=50)
    tk.Label(query_window, text="社长姓名:").place(x=50, y=100)

    entry_name = tk.Entry(query_window, width=20)
    entry_name.place(x=150, y=50)
    entry_chairman = tk.Entry(query_window, width=20)
    entry_chairman.place(x=150, y=100)

    tk.Button(query_window, text="查询", command=lambda: search_club_information(entry_name, entry_chairman)).place(
        x=150, y=150)

    query_window.mainloop()


def update_club_information(entry_name, entry_num, entry_type, entry_time, entry_chairman, entry_id, entry_password):
    club_name = entry_name.get()
    club_num = entry_num.get()
    club_type = entry_type.get()
    club_time = entry_time.get()
    club_chairman = entry_chairman.get()
    chairman_id = entry_id.get()
    chairman_password = entry_password.get()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    try:
        cursor.execute(
            """
            UPDATE 社团信息汇总
            SET num = %s, type = %s, time = %s, chairman = %s
            WHERE name = %s
            """,
            (club_num, club_type, club_time, club_chairman, club_name)
        )
        cursor.execute(
            """
            UPDATE 社长信息
            SET id = %s, password = %s
            WHERE name = %s
            """,
            (chairman_id, chairman_password, club_chairman)
        )

        db.commit()
        messagebox.showinfo("成功", "社团数据修改成功")
    except mysql.connector.Error as err:
        messagebox.showerror("错误", f"修改社团数据时发生错误: {err}")

    cursor.close()
    db.close()

    update_window.destroy()
    admin.destroy()
    admin_panel()  # 重新加载管理员面板以刷新数据


def modify_club_information():
    global update_window

    update_window = tk.Toplevel(master=admin)
    update_window.title("修改社团数据")
    update_window.geometry("500x500+500+200")
    update_window.resizable(False, False)

    modify_club_image = Image.open('秋.jpg')
    modify_club_image = modify_club_image.resize((500, 500))
    modify_club_photo = ImageTk.PhotoImage(modify_club_image)

    modify_club_background_label = tk.Label(update_window, image=modify_club_photo)
    modify_club_background_label.place(x=0, y=0)

    tk.Label(update_window, text="社团名称:").place(x=50, y=50)
    tk.Label(update_window, text="社团人数:").place(x=50, y=100)
    tk.Label(update_window, text="社团类型:").place(x=50, y=150)
    tk.Label(update_window, text="成立时间:").place(x=50, y=200)
    tk.Label(update_window, text="社长姓名:").place(x=50, y=250)
    tk.Label(update_window, text="社长学号:").place(x=50, y=300)
    tk.Label(update_window, text="密码:").place(x=50, y=350)

    entry_name = tk.Entry(update_window, width=20)
    entry_name.place(x=150, y=50)
    entry_num = tk.Entry(update_window, width=20)
    entry_num.place(x=150, y=100)
    entry_type = tk.Entry(update_window, width=20)
    entry_type.place(x=150, y=150)
    entry_time = tk.Entry(update_window, width=20)
    entry_time.place(x=150, y=200)
    entry_chairman = tk.Entry(update_window, width=20)
    entry_chairman.place(x=150, y=250)
    entry_id = tk.Entry(update_window, width=20)
    entry_id.place(x=150, y=300)
    entry_password = tk.Entry(update_window, width=20)
    entry_password.place(x=150, y=350)

    tk.Button(update_window, text="提交",
              command=lambda: update_club_information(entry_name, entry_num, entry_type, entry_time,
                                                      entry_chairman, entry_id, entry_password)).place(x=150, y=400)

    update_window.mainloop()


def add_admin_information(entry_name, entry_id, entry_password):
    admin_name = entry_name.get()
    admin_id = entry_id.get()
    admin_password = entry_password.get()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()

    try:
        cursor.execute(
            """
            INSERT INTO 管理员信息 (name, id, password)
            VALUES (%s, %s, %s)
            """,
            (admin_name, admin_id, admin_password)
        )

        db.commit()
        messagebox.showinfo("成功", "管理员信息添加成功")
    except mysql.connector.Error as err:
        messagebox.showerror("错误", f"添加管理员信息时发生错误: {err}")

    cursor.close()
    db.close()

    add_admin_window.destroy()
    admin.destroy()
    admin_panel()  # 重新加载管理员面板以刷新数据


def add_admin():
    global add_admin_window

    add_admin_window = tk.Toplevel(master=admin)
    add_admin_window.title("增加管理员信息")
    add_admin_window.geometry("500x500+500+200")
    add_admin_window.resizable(False, False)

    add_admin_image = Image.open('秋.jpg')
    add_admin_image = add_admin_image.resize((500, 500))
    add_admin_photo = ImageTk.PhotoImage(add_admin_image)

    add_admin_background_label = tk.Label(add_admin_window, image=add_admin_photo)
    add_admin_background_label.place(x=0, y=0)

    tk.Label(add_admin_window, text="管理员姓名:").place(x=50, y=50)
    tk.Label(add_admin_window, text="学号:").place(x=50, y=100)
    tk.Label(add_admin_window, text="密码:").place(x=50, y=150)

    entry_name = tk.Entry(add_admin_window, width=20)
    entry_name.place(x=150, y=50)
    entry_id = tk.Entry(add_admin_window, width=20)
    entry_id.place(x=150, y=100)
    entry_password = tk.Entry(add_admin_window, show='*', width=20)
    entry_password.place(x=150, y=150)

    tk.Button(add_admin_window, text="提交",
              command=lambda: add_admin_information(entry_name, entry_id, entry_password)).place(x=150, y=200)

    add_admin_window.mainloop()


# 管理员界面
def admin_panel():
    global admin
    admin = tk.Tk()
    admin.geometry('1000x600+400+100')
    admin.resizable(False, False)
    admin.title('管理员管理系统')
    # 其他组件和功能实现
    admin_image = Image.open('实事求是.jpg')
    admin_image = admin_image.resize((1000, 600))
    admin_photo = ImageTk.PhotoImage(admin_image)

    admin_background_label = tk.Label(admin, image=admin_photo)
    admin_background_label.place(x=0, y=0)

    tk.Label(admin, text='社团信息', font=("KaiTi", 20)).place(x=50, y=50)
    tk.Button(admin, text='添加社团数据', command=new_club_information).place(x=50, y=100)
    tk.Button(admin, text='删除社团数据', command=remove_club_information).place(x=50, y=150)
    tk.Button(admin, text='修改社团数据', command=modify_club_information).place(x=50, y=200)
    tk.Button(admin, text='查询社团数据', command=query_club_information).place(x=50, y=250)
    tk.Button(admin, text='添加管理员', command=add_admin).place(x=50, y=300)
    tk.Button(admin, text='退出', command=admin.destroy).place(x=50, y=350)

    tree = Treeview(admin, columns=('name', 'num', 'type', 'time', 'chairman', 'p_id'), show='headings')
    tree.heading('name', text='社团名称')
    tree.heading('num', text='社团人数')
    tree.heading('type', text='社团类型')
    tree.heading('time', text='成立时间')
    tree.heading('chairman', text='负责人')
    tree.heading('p_id', text='对应表编号')

    tree.place(x=200, y=50)

    information = show_information('社团信息汇总')
    tree.column('name', width=100, anchor='center')
    tree.column('num', width=100, anchor='center')
    tree.column('type', width=100, anchor='center')
    tree.column('time', width=200, anchor='center')
    tree.column('chairman', width=100, anchor='center')
    tree.column('p_id', width=100, anchor='center')

    for i in information:
        tree.insert('', 'end', values=i)

    admin.mainloop()


def submit_member_information(chairman, table_name, entry_name, entry_posts, entry_gender, entry_age, entry_academy,
                              entry_phone):
    name = entry_name.get().strip()
    posts = entry_posts.get().strip()
    gender = entry_gender.get().strip()
    age = int(entry_age.get().strip())
    academy = entry_academy.get().strip()
    phone = entry_phone.get().strip()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()
    query = f"""
    INSERT INTO {table_name} (name, posts, gender, age, academy, phone)
    VALUES (%s, %s, %s, %s, %s, %s)
    """

    try:
        cursor.execute(query, (name, posts, gender, age, academy, phone))
        db.commit()
        messagebox.showinfo("成功", "成员数据添加成功")
    except mysql.connector.Error as err:
        messagebox.showerror("数据库错误", str(err))
    finally:
        cursor.close()
        db.close()
        new_member.destroy()
        president.destroy()
        president_panel(chairman)  # 重新加载社长面板以刷新数据


def add_member_information(table_name, chair_man):
    global new_member
    new_member = tk.Toplevel(master=president)
    new_member.title("添加社团成员")
    new_member.geometry("500x500+500+200")
    new_member.resizable(False, False)

    new_member_image = Image.open('秋.jpg')
    new_member_image = new_member_image.resize((500, 500))
    new_member_photo = ImageTk.PhotoImage(new_member_image)

    new_member_background_label = tk.Label(new_member, image=new_member_photo)
    new_member_background_label.place(x=0, y=0)

    tk.Label(new_member, text="成员姓名:").place(x=50, y=50)
    tk.Label(new_member, text="职位:").place(x=50, y=100)
    tk.Label(new_member, text="性别:").place(x=50, y=150)
    tk.Label(new_member, text="年龄:").place(x=50, y=200)
    tk.Label(new_member, text="学院:").place(x=50, y=250)
    tk.Label(new_member, text="电话:").place(x=50, y=300)

    entry_name = tk.Entry(new_member, width=20)
    entry_name.place(x=150, y=50)
    entry_posts = tk.Entry(new_member, width=20)
    entry_posts.place(x=150, y=100)
    entry_gender = tk.Entry(new_member, width=20)
    entry_gender.place(x=150, y=150)
    entry_age = tk.Entry(new_member, width=20)
    entry_age.place(x=150, y=200)
    entry_academy = tk.Entry(new_member, width=20)
    entry_academy.place(x=150, y=250)
    entry_phone = tk.Entry(new_member, width=20)
    entry_phone.place(x=150, y=300)

    tk.Button(new_member, text="提交",
              command=lambda: submit_member_information(chair_man, table_name, entry_name, entry_posts, entry_gender,
                                                        entry_age, entry_academy, entry_phone)).place(x=150, y=350)

    new_member.mainloop()


def delete_member_information(table_name, entry_name, chairman):
    name = entry_name.get().strip()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()
    query = f"DELETE FROM {table_name} WHERE name = %s"

    try:
        cursor.execute(query, (name,))
        db.commit()
        messagebox.showinfo("成功", "成员数据删除成功")
    except mysql.connector.Error as err:
        messagebox.showerror("数据库错误", str(err))
    finally:
        cursor.close()
        db.close()
        delete_member.destroy()
        president.destroy()
        president_panel(chairman)  # 重新加载社长面板以刷新数据


def remove_member_information(table_name, chairman):
    global delete_member
    delete_member = tk.Toplevel(master=president)
    delete_member.title("删除社团成员")
    delete_member.geometry("500x500+500+200")
    delete_member.resizable(False, False)

    delete_member_image = Image.open('秋.jpg')
    delete_member_image = delete_member_image.resize((500, 500))
    delete_member_photo = ImageTk.PhotoImage(delete_member_image)

    delete_member_background_label = tk.Label(delete_member, image=delete_member_photo)
    delete_member_background_label.place(x=0, y=0)

    tk.Label(delete_member, text="成员姓名:").place(x=50, y=50)

    entry_name = tk.Entry(delete_member, width=20)
    entry_name.place(x=150, y=50)

    tk.Button(delete_member, text="提交",
              command=lambda: delete_member_information(table_name, entry_name, chairman)).place(x=150, y=100)

    delete_member.mainloop()

def update_member_information(chairman, table_name, entry_old_name, entry_new_name, entry_posts, entry_gender, entry_age, entry_academy, entry_phone):
    old_name = entry_old_name.get().strip()
    new_name = entry_new_name.get().strip()
    posts = entry_posts.get().strip()
    gender = entry_gender.get().strip()
    age = int(entry_age.get().strip())
    academy = entry_academy.get().strip()
    phone = entry_phone.get().strip()

    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    cursor = db.cursor()
    query = f"""
    UPDATE {table_name}
    SET name = %s, posts = %s, gender = %s, age = %s, academy = %s, phone = %s
    WHERE name = %s
    """

    try:
        cursor.execute(query, (new_name, posts, gender, age, academy, phone, old_name))
        db.commit()
        messagebox.showinfo("成功", "成员数据修改成功")
    except mysql.connector.Error as err:
        messagebox.showerror("数据库错误", str(err))
    finally:
        cursor.close()
        db.close()
        update_member.destroy()
        president.destroy()
        president_panel(chairman)  # 重新加载社长面板以刷新数据


def modify_member_information(table_name, chairman):
    global update_member
    update_member = tk.Toplevel(master=president)
    update_member.title("修改社团成员")
    update_member.geometry("500x500+500+200")
    update_member.resizable(False, False)

    update_member_image = Image.open('秋.jpg')
    update_member_image = update_member_image.resize((500, 500))
    update_member_photo = ImageTk.PhotoImage(update_member_image)

    update_member_background_label = tk.Label(update_member, image=update_member_photo)
    update_member_background_label.place(x=0, y=0)

    tk.Label(update_member, text="原成员姓名:").place(x=50, y=50)
    tk.Label(update_member, text="新成员姓名:").place(x=50, y=100)
    tk.Label(update_member, text="职位:").place(x=50, y=150)
    tk.Label(update_member, text="性别:").place(x=50, y=200)
    tk.Label(update_member, text="年龄:").place(x=50, y=250)
    tk.Label(update_member, text="学院:").place(x=50, y=300)
    tk.Label(update_member, text="电话:").place(x=50, y=350)

    entry_old_name = tk.Entry(update_member, width=20)
    entry_old_name.place(x=150, y=50)
    entry_new_name = tk.Entry(update_member, width=20)
    entry_new_name.place(x=150, y=100)
    entry_posts = tk.Entry(update_member, width=20)
    entry_posts.place(x=150, y=150)
    entry_gender = tk.Entry(update_member, width=20)
    entry_gender.place(x=150, y=200)
    entry_age = tk.Entry(update_member, width=20)
    entry_age.place(x=150, y=250)
    entry_academy = tk.Entry(update_member, width=20)
    entry_academy.place(x=150, y=300)
    entry_phone = tk.Entry(update_member, width=20)
    entry_phone.place(x=150, y=350)

    tk.Button(update_member, text="提交",
              command=lambda: update_member_information(chairman, table_name, entry_old_name, entry_new_name, entry_posts, entry_gender, entry_age, entry_academy, entry_phone)).place(x=150, y=400)

    update_member.mainloop()

# 社长界面
def president_panel(chairman=None):
    global president
    president = tk.Tk()
    president.geometry('1000x600+400+100')
    president.resizable(False, False)
    president.title('社长管理系统')

    # 其他组件和功能实现
    president_image = Image.open('实事求是.jpg')
    president_image = president_image.resize((1000, 600))
    president_photo = ImageTk.PhotoImage(president_image)

    president_background_label = tk.Label(president, image=president_photo)
    president_background_label.place(x=0, y=0)

    tree = Treeview(president, columns=('name', 'posts', 'gender', 'age', 'academy', 'phone'), show='headings')
    tree.heading('name', text='成员姓名')
    tree.heading('posts', text='职位')
    tree.heading('gender', text='性别')
    tree.heading('age', text='年纪')
    tree.heading('academy', text='学院')
    tree.heading('phone', text='电话')

    tree.place(x=200, y=50)

    # 创建到数据库的连接
    db = mysql.connector.connect(
        host="localhost",
        user='root',
        password="*******",
        database="infsys"
    )

    # 创建一个游标对象
    cursor = db.cursor()

    # 执行SQL查询
    cursor.execute("SELECT * FROM 社团信息汇总 WHERE chairman = %s", (chairman,))

    # 获取查询结果
    results = cursor.fetchall()

    # 关闭游标和连接
    cursor.close()
    db.close()

    club = results[0][0]

    information = show_information(club)

    tree.column('name', width=100, anchor='center')
    tree.column('posts', width=100, anchor='center')
    tree.column('gender', width=100, anchor='center')
    tree.column('age', width=100, anchor='center')
    tree.column('academy', width=100, anchor='center')
    tree.column('phone', width=100, anchor='center')

    for i in information:
        tree.insert('', 'end', values=i)

    tk.Label(president, text='社团信息', font=("KaiTi", 20)).place(x=50, y=50)
    tk.Button(president, text='添加成员数据', command=lambda: add_member_information(club, chairman)).place(x=50, y=100)
    tk.Button(president, text='删除成员数据', command=lambda: remove_member_information(club, chairman)).place(x=50,
                                                                                                               y=150)
    tk.Button(president, text='修改成员数据', command=lambda: modify_member_information(club, chairman)).place(x=50, y=200)
    tk.Button(president, text='退出', command=president.destroy).place(x=50, y=250)

    president.mainloop()


if __name__ == '__main__':
    #CreateDatabase()  # 先创建数据库和表
    #InsertData()  # 然后插入数据

    root = tk.Tk()
    root.geometry('500x500+400+100')
    root.resizable(False, False)
    root.title('社团管理系统')

    image = Image.open("红楼.jpg")
    image = image.resize((500, 500))
    photo = ImageTk.PhotoImage(image)

    background_label = tk.Label(root, image=photo)
    background_label.place(x=0, y=0)

    Roles = [
        ("管理员", 1),
        ("社长", 2)
    ]

    role_var = IntVar()

    for key, value in Roles:
        tk.Radiobutton(root, text=key, variable=role_var, value=value).place(x=160 + 120 * (value - 1), y=200)

    tk.Label(root, text="欢迎使用社团管理系统", font=("KaiTi", 30)).place(x=50, y=100)
    tk.Label(root, text="用户名:").place(x=100, y=250)
    tk.Label(root, text="学号:").place(x=100, y=290)
    tk.Label(root, text="密码:").place(x=100, y=330)

    entry_username = tk.Entry(root, width=25)
    entry_username.place(x=160, y=250)
    entry_school = tk.Entry(root, width=25)
    entry_school.place(x=160, y=290)
    entry_password = tk.Entry(root, show='*', width=25)
    entry_password.place(x=160, y=330)

    tk.Button(root, text="登录", command=login, width=10, height=1).place(x=160, y=370)
    tk.Button(root, text="退出", command=root.destroy, width=10, height=1).place(x=260, y=370)

    root.mainloop()


原文地址:https://blog.csdn.net/weixin_73654895/article/details/140415221

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!