本次我们小组作业选题为:信息管理系统。

原本依着简单好写的期待,选择了这个选题,然后老师给我们建议的功能加到了11项,我看了看都不算太难就全部留下了。历时六个小时,目前程序已经基本写完(只差一项,就是UI太丑了),包括登陆页面,注册页面,用户主页面,查看个人信息页面,查看他人信息页面,管理员页面等。包含用户的注册,登录,查看,修改,清空等功能,还有管理员的查看用户列表,删除用户文件等。

以下我将附上运行的代码及运行截图:

1.登录.py

from tkinter import *
from tkinter import messagebox
import subprocess
import os

root = Tk()
root.title('信息管理系统')
root.geometry('350x300+700+250')

'创建一个标签并设置居中文字'
label = Label(root, text='信息管理系统', font=('黑体', 24), bg='lightblue', fg='white')
label.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

'创建用户名和密码输入框'
username = StringVar()
password = StringVar()

u_label = Label(root, text='用户名:', font=('黑体', 12), bg='lightgray')
u_label.grid(row=1, column=0, pady=10, sticky='w')

u_username = Entry(root, textvariable=username, font=('黑体', 12))
u_username.grid(row=1, column=1)

p_label = Label(root, text='密  码:', font=('黑体', 12), bg='lightgray')
p_label.grid(row=2, column=0, pady=10, sticky='w')

p_password = Entry(root, textvariable=password, show='*', font=('黑体', 12))
p_password.grid(row=2, column=1)

'创建登录按钮'
bt1 = Button(root, text='登录', font=('黑体', 12), bg='blue', fg='white')
bt1.grid(row=5, column=0, columnspan=2,padx=(20,10))
bt2 = Button(root, text='注册', font=('黑体', 12), bg='#0000ff', fg='#fffff0')
bt2.grid(row=5, column=1, columnspan=1,padx=(50,10))

def Password_Confirmation():
    global username,password
    '将输入框内的姓名和密码提取到变量'
    use_usename = username.get()
    pass_password = password.get()

    user_file = f"{use_usename}.txt"
    '用os检查用户名.txt文件是否存在'
    if os.path.exists(user_file):
        with open(user_file, 'r', encoding='utf-8') as file_read_pw:
            content = file_read_pw.readlines()
            '假设密码在第四行'
            really_password = content[3].split(':')[1].strip()
        if use_usename=="Hestia":
            '比较存储的密码和输入的密码'
            if pass_password == really_password:
                messagebox.showinfo("管理员身份验证成功", "欢迎您的登录!")
                subprocess.run(["python", r"管理员_Hestia专用页面.py"])
                root.destroy()
            else:
                messagebox.showerror("大胆!!!", "不是管理员你想干嘛!")
        else:
            '比较存储的密码和输入的密码'
            if pass_password == really_password:
                messagebox.showinfo("登录成功", "欢迎登录!")
                subprocess.run(["python",r"主页面.py",use_usename])
                root.destroy()
            else:
                messagebox.showerror("登录失败", "密码错误!")
    else:
        messagebox.showerror("登录失败", "用户名不存在!")

def c2(event):
    Password_Confirmation()
bt1.bind('<Button-1>', c2)
def c3(event):
    name = ''
    text = '点击确定进入注册页面'
    if messagebox.askokcancel(name, text):
        subprocess.run(["python", "注册页面.py"])
bt2.bind('<Button-1>', c3)

root.mainloop()

2.注册.py

from tkinter import *
from tkinter import messagebox
import os

root = Tk()
root.title('注册')
root.geometry('300x400+700+300')

'信息录入'
x_name = StringVar()
sex = StringVar()
id = StringVar()
password = StringVar()
phone = StringVar()
year = StringVar()

label = Label(root, text='注册信息', font=('黑体', 24), bg='lightblue', fg='white')
label.grid(row=0, column=0, columnspan=2, sticky='ew', padx=10, pady=10)
'姓名'
name_label = Label(root, text = '姓名:', font=('黑体', 12), bg='lightgray')
name_label.grid(row=1, column=0, pady=10)
n_name = Entry(root, textvariable=x_name, font=('黑体', 12))
n_name.grid(row=1, column=1, pady=10)
'性别'
sex_label = Label(root, text = '性别:', font=('黑体', 12), bg='lightgray')
sex_label.grid(row=3, column=0, pady=10)
s_sex = Entry(root, textvariable=sex, font=('黑体', 12))
s_sex.grid(row=3, column=1, pady=10)
'学号'
id_label = Label(root, text = '学号:', font=('黑体', 12), bg='lightgray')
id_label.grid(row=5, column=0, pady=10)
i_id = Entry(root, textvariable=id, font=('黑体', 12))
i_id.grid(row=5, column=1, pady=10)
'密码'
password_label = Label(root, text = '密码:', font=('黑体', 12), bg='lightgray')
password_label.grid(row=7, column=0, pady=10)
password_id = Entry(root, textvariable=password, font=('黑体', 12))
password_id.grid(row=7, column=1, pady=10)
'联系电话'
phone_label = Label(root, text = '联系电话:', font=('黑体', 12), bg='lightgray')
phone_label.grid(row=9, column=0, pady=10)
p_phone = Entry(root, textvariable=phone, font=('黑体', 12))
p_phone.grid(row=9, column=1, pady=10)
'入学年份'
year_label = Label(root, text = '入学年份:', font=('黑体', 12), bg='lightgray')
year_label.grid(row=11, column=0, pady=10)
y_year = Entry(root, textvariable=year, font=('黑体', 12))
y_year.grid(row=11, column=1, pady=10)

public_name=IntVar()
public_sex=IntVar()
public_id=IntVar()
public_password=IntVar()
public_phone=IntVar()
public_year=IntVar()

def gaibian_color(button, colors):
    current_color = button.cget("bg")
    new_color = colors[0] if current_color == colors[1] else colors[1]
    button.config(bg=new_color)

# 初始化颜色,第一个颜色为默认颜色,第二个颜色为切换后的颜色
name_public_colors = ['SystemButtonFace', '#ffc0cb']
sex_public_colors = ['SystemButtonFace', '#ffc0cb']
id_public_colors = ['SystemButtonFace', '#ffc0cb']
password_public_colors = ['SystemButtonFace', '#ffc0cb']
phone_public_colors = ['SystemButtonFace', '#ffc0cb']
year_public_colors = ['SystemButtonFace', '#ffc0cb']

def name_public():
    public_name.set(1 - public_name.get())
    gaibian_color(name_public_bt, name_public_colors)
name_public_bt = Button(root, text='公开', font=('黑体', 12), command=name_public)
name_public_bt.grid(row=1, column=2, pady=10)

def sex_public():
    public_sex.set(1 - public_sex.get())
    gaibian_color(sex_public_bt, sex_public_colors)
sex_public_bt = Button(root, text='公开', font=('黑体', 12), command=sex_public )
sex_public_bt.grid(row=3, column=2, pady=10)

def id_public():
    public_id.set(1 - public_id.get())
    gaibian_color(id_public_bt, id_public_colors)
id_public_bt = Button(root, text='公开', font=('黑体', 12), command=id_public)
id_public_bt.grid(row=5, column=2, pady=10)

def password_public():
    public_password.set(1 - public_password.get())
    gaibian_color(password_public_bt, password_public_colors)
password_public_bt = Button(root, text='公开', font=('黑体', 12), command=password_public)
password_public_bt.grid(row=7, column=2, pady=10)

def phone_public():
    public_phone.set(1 - public_phone.get())
    gaibian_color(phone_public_bt, phone_public_colors)
phone_public_bt = Button(root, text='公开', font=('黑体', 12), command=phone_public)
phone_public_bt.grid(row=9, column=2, pady=10)

def year_public():
    public_year.set(1 - public_year.get())
    gaibian_color(year_public_bt, year_public_colors)
year_public_bt = Button(root, text='公开', font=('黑体', 12), command=year_public)
year_public_bt.grid(row=11, column=2, pady=10)

'创建提交按钮'
bt1 = Button(root, text='提交', font=('黑体', 12), bg='blue', fg='white')
bt1.grid(row=13, column=0, columnspan=2,padx=(20,10))

def c2(event):
    #将录入的信息变量声明为全局变量
    global x_name, sex, id, phone, year,password
    u_name = x_name.get()
    u_sex = sex.get()
    u_id = id.get()
    u_password = password.get()
    u_phone = phone.get()
    u_year = year.get()

    file = f"{u_name}.txt"
    file_public=f"{u_name}_public.txt"

    # 检查文件是否存在,如果存在则删除
    if os.path.exists(file):
        os.remove(file)

    with open(file, 'a', encoding='utf-8') as file_write:
        file_write.write(f"姓名:{u_name}\n")
        file_write.write(f"性别:{u_sex}\n")
        file_write.write(f"学号:{u_id}\n")
        file_write.write(f"密码:{u_password}\n")
        file_write.write(f"联系电话:{u_phone}\n")
        file_write.write(f"入学年份:{u_year}\n")

    with open(file_public, 'a', encoding='utf-8') as file_write_public:
        if (public_name.get()==1):
             file_write_public.write(f"姓名:{u_name}\n")
        if (public_sex.get()==1):
             file_write_public.write(f"性别:{u_sex}\n")
        if (public_id.get()==1):
             file_write_public.write(f"学号:{u_id}\n")
        if (public_password.get()==1):
             file_write_public.write(f"密码:{u_password}\n")
        if (public_phone.get()==1):
             file_write_public.write(f"联系电话:{u_phone}\n")
        if (public_year.get()==1):
            file_write_public.write(f"入学年份:{u_year}\n")

    name = ''
    text = '注册成功!'
    messagebox.showinfo(name, text)
    #read()

bt1.bind('<Button-1>', c2)

#输出文件内容函数,用于测试是否输入成功
def read():
    u_name = x_name.get()
    file = f"{u_name}.txt"
    with open(file, 'r',encoding='utf-8') as file_read:
        content = file_read.read()
        print(content)

root.mainloop()

3.主页面.py

from tkinter import *
from tkinter import messagebox
import subprocess
import sys
import os

root=Tk()
root.title('信息管理系统')
root.geometry('500x400+700+300')


if len(sys.argv)>1:
    username = sys.argv[1]
else:
    print("无用户信息")
user_file=f"{username}.txt"
with open(user_file, 'r',encoding='utf-8') as file_read:
    content = file_read.readlines()
    name = content[0].split(':')[1].strip()


biaoti = Label(root, text=f"{name} 欢迎你使用本系统!!!", font=('黑体', 24))
#biaoti = Label(root, text=f"欢迎你使用本系统!!!", font=('黑体', 24))
biaoti.grid(row=0, column=0, columnspan=8, padx=10, pady=10)

tishi = Label(root, text='请选择你要使用的功能:', font=('黑体', 16))
tishi.grid(row=1, column=0, columnspan=1, padx=10, pady=10)

ckgrxx = Button(root, text='查看个人信息', font=('黑体', 12), bg='blue', fg='white')
ckgrxx.grid(row=5, column=0, columnspan=1,padx=(5,10))

xggrxx = Button(root, text='修改个人信息', font=('黑体', 12), bg='blue', fg='white')
xggrxx.grid(row=6, column=0, columnspan=1,padx=(5,10))

qkgrxx = Button(root, text='清空个人信息', font=('黑体', 12), bg='blue', fg='white')
qkgrxx.grid(row=7, column=0, columnspan=1,padx=(5,10))

cktrxx = Button(root, text='查看他人信息', font=('黑体', 12), bg='blue', fg='white')
cktrxx.grid(row=8, column=0, columnspan=1,padx=(5,10))

tuichu = Button(root, text='退出系统', font=('黑体', 12), bg='blue', fg='white')
tuichu.grid(row=9, column=0, columnspan=1,padx=(5,10))

def ck1(event):
    name='查看个人信息'
    text='点击确定打开查看个人信息页面'
    messagebox.showinfo(name, text)
    subprocess.run(["python", r"查看个人信息页面.py",username])
ckgrxx.bind('<Button-1>', ck1)

def ck2(event):
    name='删除个人信息'
    text='确定要删除个人信息吗?'
    if messagebox.askokcancel(name, text):
        scxx()
qkgrxx.bind('<Button-1>', ck2)
use_file="1.txt"
def scxx():
    with open(use_file, 'w', encoding='utf-8') as file_write:
        file_write.write(f"姓名: \n")
        file_write.write(f"性别: \n")
        file_write.write(f"学号: \n")
        file_write.write(f"密码: \n")
        file_write.write(f"联系电话: \n")
        file_write.write(f"入学年份: \n")

def ck3(event):
    name='退出'
    text='确定要退出系统吗?'
    if messagebox.askokcancel(name, text):
        exit()
tuichu.bind('<Button-1>', ck3)

def ck4(event):
    name='修改个人信息'
    text='点击确定打开修改信息页面'
    messagebox.showinfo(name, text)
    subprocess.run(["python", r"修改信息页面.py"])
xggrxx.bind('<Button-1>', ck4)
def ck5(event):
    name='查看他人信息'
    text='点击确定打开查看他人信息页面'
    messagebox.showinfo(name, text)
    subprocess.run(["python", r"查看他人信息页面.py"])
cktrxx.bind('<Button-1>', ck5)

root.mainloop()

4.查看个人信息.py

from tkinter import *
from tkinter import Text
from tkinter import messagebox
import os
import subprocess
import sys

root=Tk()
root.title('个人信息')
root.geometry('250x200+700+300')

def save_file():
    content = xx_box.get("1.0", END)  # 获取文本框内容
    save_file_ = os.path.join(wjlj, f"{username}_save.txt")
    with open(save_file_, 'w', encoding='utf-8') as file_save:
        file_save.write(content)
    messagebox.showinfo("导出成功", f"内容已导出到 {save_file_}")


#查询传递的用户名:
if len(sys.argv)>1:
    username = sys.argv[1]
else:
    print("无用户信息")

user_file = f"{username}.txt"

xx_box = Text(root, height=10, width=40)
xx_box.pack(padx=10, pady=10)

with open(user_file, 'r', encoding='utf-8') as file:  #打开文件
    content = file.read()  # 读取文件内容
    xx_box.insert(END, content)

export_button = Button(root, text='导出', font=('黑体', 12), bg='green', fg='white', command=save_file)
export_button.pack(side=BOTTOM, pady=10)

root.mainloop()

5.查看他人个人信息.py

from tkinter import *
from tkinter import messagebox
import os

def qk():
    # 清空当前页面
    for widget in root.winfo_children():
        widget.destroy()

    # 获取用户输入的姓名
    username = ck_name.get()
    user_file = f"{username}_public.txt"

    # 创建新的文本框来显示信息
    xx_box = Text(root, height=10, width=40)
    xx_box.pack(padx=10, pady=10)

    try:
        with open(user_file, 'r', encoding='utf-8') as file:  # 打开文件
            content = file.read()  # 读取文件内容
            xx_box.insert(END, content)
    except FileNotFoundError:
        messagebox.showerror("错误", "没有找到用户信息文件!")
        exit()

root = Tk()
root.title('个人信息')
root.geometry('250x200+700+300')

ck_name = StringVar()
name_label = Label(root, text='请输入你要查看的姓名:', font=('黑体', 12), bg='lightgray')
name_label.grid(row=1, column=0, pady=10)
n_name = Entry(root, textvariable=ck_name, font=('黑体', 12))
n_name.grid(row=1, column=1, pady=10)

# 创建提交按钮
bt1 = Button(root, text='提交', font=('黑体', 12), bg='blue', fg='white', command=qk)
bt1.grid(row=13, column=0, columnspan=2, padx=(20, 10))

root.mainloop()

6.修改信息页面(偷了个懒 修改信息就是重新注册 嘿嘿)

from tkinter import *
from tkinter import messagebox
import os

root = Tk()
root.title('修改信息')
root.geometry('300x400+700+300')

'信息录入'
x_name = StringVar()
sex = StringVar()
id = StringVar()
password = StringVar()
phone = StringVar()
year = StringVar()

label = Label(root, text='修改信息:', font=('黑体', 24), bg='lightblue', fg='white')
label.grid(row=0, column=0, columnspan=2, sticky='ew', padx=10, pady=10)
'姓名'
name_label = Label(root, text = '姓名:', font=('黑体', 12), bg='lightgray')
name_label.grid(row=1, column=0, pady=10)
n_name = Entry(root, textvariable=x_name, font=('黑体', 12))
n_name.grid(row=1, column=1, pady=10)
'性别'
sex_label = Label(root, text = '性别:', font=('黑体', 12), bg='lightgray')
sex_label.grid(row=3, column=0, pady=10)
s_sex = Entry(root, textvariable=sex, font=('黑体', 12))
s_sex.grid(row=3, column=1, pady=10)
'学号'
id_label = Label(root, text = '学号:', font=('黑体', 12), bg='lightgray')
id_label.grid(row=5, column=0, pady=10)
i_id = Entry(root, textvariable=id, font=('黑体', 12))
i_id.grid(row=5, column=1, pady=10)
'密码'
password_label = Label(root, text = '密码:', font=('黑体', 12), bg='lightgray')
password_label.grid(row=7, column=0, pady=10)
password_id = Entry(root, textvariable=password, font=('黑体', 12))
password_id.grid(row=7, column=1, pady=10)
'联系电话'
phone_label = Label(root, text = '联系电话:', font=('黑体', 12), bg='lightgray')
phone_label.grid(row=9, column=0, pady=10)
p_phone = Entry(root, textvariable=phone, font=('黑体', 12))
p_phone.grid(row=9, column=1, pady=10)
'入学年份'
year_label = Label(root, text = '入学年份:', font=('黑体', 12), bg='lightgray')
year_label.grid(row=11, column=0, pady=10)
y_year = Entry(root, textvariable=year, font=('黑体', 12))
y_year.grid(row=11, column=1, pady=10)

public_name=IntVar()
public_sex=IntVar()
public_id=IntVar()
public_password=IntVar()
public_phone=IntVar()
public_year=IntVar()

def gaibian_color(button, colors):
    current_color = button.cget("bg")
    new_color = colors[0] if current_color == colors[1] else colors[1]
    button.config(bg=new_color)

# 初始化颜色,第一个颜色为默认颜色,第二个颜色为切换后的颜色
name_public_colors = ['SystemButtonFace', '#ffc0cb']
sex_public_colors = ['SystemButtonFace', '#ffc0cb']
id_public_colors = ['SystemButtonFace', '#ffc0cb']
password_public_colors = ['SystemButtonFace', '#ffc0cb']
phone_public_colors = ['SystemButtonFace', '#ffc0cb']
year_public_colors = ['SystemButtonFace', '#ffc0cb']

def name_public():
    public_name.set(1 - public_name.get())
    gaibian_color(name_public_bt, name_public_colors)
name_public_bt = Button(root, text='公开', font=('黑体', 12), command=name_public)
name_public_bt.grid(row=1, column=2, pady=10)

def sex_public():
    public_sex.set(1 - public_sex.get())
    gaibian_color(sex_public_bt, sex_public_colors)
sex_public_bt = Button(root, text='公开', font=('黑体', 12), command=sex_public )
sex_public_bt.grid(row=3, column=2, pady=10)

def id_public():
    public_id.set(1 - public_id.get())
    gaibian_color(id_public_bt, id_public_colors)
id_public_bt = Button(root, text='公开', font=('黑体', 12), command=id_public)
id_public_bt.grid(row=5, column=2, pady=10)

def password_public():
    public_password.set(1 - public_password.get())
    gaibian_color(password_public_bt, password_public_colors)
password_public_bt = Button(root, text='公开', font=('黑体', 12), command=password_public)
password_public_bt.grid(row=7, column=2, pady=10)

def phone_public():
    public_phone.set(1 - public_phone.get())
    gaibian_color(phone_public_bt, phone_public_colors)
phone_public_bt = Button(root, text='公开', font=('黑体', 12), command=phone_public)
phone_public_bt.grid(row=9, column=2, pady=10)

def year_public():
    public_year.set(1 - public_year.get())
    gaibian_color(year_public_bt, year_public_colors)
year_public_bt = Button(root, text='公开', font=('黑体', 12), command=year_public)
year_public_bt.grid(row=11, column=2, pady=10)

'创建提交按钮'
bt1 = Button(root, text='提交', font=('黑体', 12), bg='blue', fg='white')
bt1.grid(row=13, column=0, columnspan=2,padx=(20,10))

def c2(event):
    #将录入的信息变量声明为全局变量
    global x_name, sex, id, phone, year,password
    u_name = x_name.get()
    u_sex = sex.get()
    u_id = id.get()
    u_password = password.get()
    u_phone = phone.get()
    u_year = year.get()

    file = f"{u_name}.txt"
    file_public=f"{u_name}_public.txt"

    # 检查文件是否存在,如果存在则删除
    if os.path.exists(file):
        os.remove(file)

    with open(file, 'a', encoding='utf-8') as file_write:
        file_write.write(f"姓名:{u_name}\n")
        file_write.write(f"性别:{u_sex}\n")
        file_write.write(f"学号:{u_id}\n")
        file_write.write(f"密码:{u_password}\n")
        file_write.write(f"联系电话:{u_phone}\n")
        file_write.write(f"入学年份:{u_year}\n")

    with open(file_public, 'a', encoding='utf-8') as file_write_public:
        if (public_name.get()==1):
             file_write_public.write(f"姓名:{u_name}\n")
        if (public_sex.get()==1):
             file_write_public.write(f"性别:{u_sex}\n")
        if (public_id.get()==1):
             file_write_public.write(f"学号:{u_id}\n")
        if (public_password.get()==1):
             file_write_public.write(f"密码:{u_password}\n")
        if (public_phone.get()==1):
             file_write_public.write(f"联系电话:{u_phone}\n")
        if (public_year.get()==1):
            file_write_public.write(f"入学年份:{u_year}\n")

    name = ''
    text = '修改成功!'
    messagebox.showinfo(name, text)
    #read()

bt1.bind('<Button-1>', c2)





#输出文件内容函数,用于测试是否输入成功
def read():
    u_name = x_name.get()
    file = f"{u_name}.txt"
    with open(file, 'r',encoding='utf-8') as file_read:
        content = file_read.read()
        print(content)

root.mainloop()

7.管理员页面.py

from tkinter import *
from tkinter import Text
from tkinter import messagebox
import os
import subprocess
import sys
from tkinter import simpledialog

root=Tk()
root.title('管理员系统')
root.geometry('250x500+700+300')

def delete_user():
    delete_name = simpledialog.askstring("删除用户", "请输入要删除的用户名:", parent=root)
    if delete_name=="Hestia":
        messagebox.showwarning("不是哥们", f"你删自个干什么??")
    else:
        delete_name_file_public=f"{delete_name}.txt"
        delete_name_file_self=f"{delete_name}_public.txt"
        if os.path.exists(delete_name_file_public):
            os.remove(delete_name_file_public)
        else:
            messagebox.showwarning("警告", f"用户 {delete_name} 不存在。")

        if os.path.exists(delete_name_file_self):
            os.remove(delete_name_file_self)
            messagebox.showinfo("成功删除", f"{delete_name}个人账号已删除")
        hqyh()

yhxx = Text(root, height=30, width=40)
yhxx.pack(padx=10, pady=10)
def hqyh():
    yhxx.delete('1.0', END)
    # 获取当前目录下的所有文件和文件夹名
    entries = os.listdir('.')
    # 筛选出所有以.txt结尾的文件名
    txt_files = [file for file in entries if file.endswith('.txt')]

    for name in txt_files:
        user_name=os.path.splitext(name)[0]
        yhxx.insert(END, user_name + '\n')  # 在每个文件名后添加换行符
hqyh()
export_button = Button(root, text='删除用户', font=('黑体', 12), bg='#ffc0cb', fg='black', command=delete_user)
export_button.pack(side=BOTTOM, pady=10)
root.mainloop()

接下来附上运行截图:(不展示全貌)

小结:

以上就是本次我写的信息管理系统。当然可以优化的地方还有很多,偷的懒也可以仔细展开写,尤其是UI,鉴于我没什么审美的,所以弄得很丑,当然有机会再改嘛。

作为第一篇计划中的分享学习的文章,正好碰上周末写完此程序,所以发出当作第一篇。当然关于代码部分,当然不会止步于此,休整一段时间后我也会尝试更难的程序,也尽量少取巧,这篇文章虽长,可能比其他类型的分析文章长许多许多,但是肯定不是最长。

愿我们都能在月光下奔跑,早日追逐到属于自己的太阳。祝好。

将悲伤的事对半分吧,将快乐的事拼凑成双吧