本次我们小组作业选题为:信息管理系统。
本文为修改后的定稿部分,至于最初的思路和情况,详见#YM0001的说明。
选题思路:之所以选择写一个信息管理系统,原本是最开始学习c++,java时都是以信息管理系统结束基础语法的学习,本次小组作业在最初选题时便有意向往这方面写,然后还学习了UI设计,所以就区别于前两次是直接运行的,这次使用了不同页面的跳转来实现该系统。
功能分析:本项目共有8个页面,其中包括,用户登录,用户注册,用户修改信息,用户清除个人信息,用户查看个人信息,用户查看他人信息,用户导出个人信息,用户添加个人日志,管理员查看所有成员,管理员删除指定用户约10个功能。
详细分析:
1.登陆页面:设置用户名和密码输入框和登录按钮,根据用户输入的用户名和密码进入系统文件查找匹配登录。指定一个用户Hestia,若输入的密码正确则进入管理员专用页面。
2.注册页面:包括各种信息的填写框和注册按钮,每个填写框后添加一个按钮,点击按钮后实现该内容的公开处理,即该内容可以在查看他人信息页面中被其他人或自己查看到,点击公开按钮后按钮变色,变色表示公开,默认不公开。
3.主页面:包含六项功能按钮:查看,修改,清空,查看,添加日志,退出系统。点击不同按钮后跳转或执行对应功能。
4.查看个人信息页面:设置一个文本框,读取自己的个人信息文档,设置导出按钮,导出文本框内容到另一个文件(相当于折腾了半天只是修改了一下文件名,鸡肋)
5.修改个人信息页面:包括各种信息的填写框和注册按钮,每个填写框后添加一个按钮,点击按钮后实现该内容的公开处理,即该内容可以在查看他人信息页面中被其他人或自己查看到,点击公开按钮后按钮变色,变色表示公开,默认不公开。(到此为止相当于直接重新注册)然后在新的个人信息文件中再次添加写过的日志文件。
6.查看他人信息页面:包含查看信息的用户名的输入框和查询按钮,功能是调用该用户的公开资料。
7.添加个人日志页面:包含日志的日期和填写日志的输入框,然后还有是否公开的按钮,功能和注册时的公开按钮相似,还有提交按钮。提交后在原个人信息末尾添加日志部分,创建两个新文件只存储个人日志和公开日志。(凑数功能,不然显得内容比较少,其实是写记账本项目有感添加的功能,其实可以再次优化添加功能,摸了)。
8.管理员专用页面:包含文本框显示所有用户的文件,包含一个删除用户按钮,点击后输入用户名称就可以删除和该用户有关的所有文件。
项目总结:
一共用了没有10小时的小项目,思路上采用之前的系统的实现思路,然后各种功能的实现,带着思路去网络或者AI寻找功能的具体实现代码形式,然后自行细化修改。系统其实还有很多可以优化的地方,例如注册的信息可以写入数据库中,通过数据库管理数据较为方便和便于管理。例如个人日志方面可以继续添加功能,添加个人日志的搜索功能,删除功能等,这些功能在记账本系统中简单尝试过了,就不再重复,主要是懒得写了,9人小组8个页面10个功能,足矣。
写着玩的小项目,几乎没有实用性,以后学习前后端后或许以此为基础写一个例如高校信息管理系统(类本校OA)的网页,功能部分可能还不如老师给的两个项目有含金量,更不如森哥组的外卖系统复杂,优点是比较简单,用了一个周六的晚上和周天的上午,和一个周三的晚上基本完工。也有小bug,比如我的公开文件的信息是反着的(个人日志在上,个人信息在下),研究了一会也没什么结果,就摸了不管了,有兴趣找老师帮忙修正吧。
2024.12.4 本项目基本完工。
代码如下:
1.登陆页面:
import tkinter
from tkinter import *
from tkinter import messagebox
import subprocess
import os
root = Tk()
root.title('信息管理系统') #设置主窗口标题
root.geometry('500x400') #设置主窗口大小
root.maxsize(500,300)
root.minsize(500,300)
# 加载图片
canvas = tkinter.Canvas(root, width=500, height=300, bg=None)
image_file = tkinter.PhotoImage(file="2.gif")
image = canvas.create_image(250, 0, anchor='n', image=image_file)
canvas.pack()
#账号与密码文字标签
account_lable = tkinter.Label(root, text = '账号', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)
account_lable.place(relx=0.29,rely=0.4)
pasw_lable = tkinter.Label(root, text = '密码', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)
pasw_lable.place(relx=0.29,rely=0.5)
#账号与密码输入框
username = tkinter.Entry(root,width=20,highlightthickness = 1,highlightcolor = 'lightskyblue',relief='groove') #账号输入框
username.place(relx=0.4,rely=0.4 ) #添加进主页面,relx和rely意思是与父元件的相对位置
password = tkinter.Entry(root,show='*',highlightthickness = 1,highlightcolor = 'lightskyblue',relief='groove') #密码输入框
password.place(relx=0.4,rely=0.5) #添加进主页面
bt1 = tkinter.Button(root,text='登录',font = ('宋体',12),width=4,height=1,relief='solid',bd = 0.5,bg='lightcyan')
bt1.place(relx=0.41,rely=0.63)
bt2 = tkinter.Button(root,text='注册',font = ('宋体',12),width=4,height=1,bd=0.5,relief='solid',bg='lightcyan')
bt2.place(relx=0.56,rely=0.63)
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):
subprocess.run(["python", "注册页面.py"])
bt2.bind('<Button-1>', c3)
root.mainloop()
2.注册页面:
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)
if os.path.exists(file_public):
os.remove(file_public)
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)
root.destroy()
#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.主页面:
from tkinter import *
from tkinter import messagebox
import subprocess
import sys
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))
tjrz = Button(root, text='添加个人日志', font=('黑体', 12), bg='blue', fg='white')
tjrz.grid(row=9, column=0, columnspan=1,padx=(5,10))
tuichu = Button(root, text='退出系统', font=('黑体', 12), bg='blue', fg='white')
tuichu.grid(row=10, column=0, columnspan=1,padx=(5,10))
def ck1(event):
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 ck6(event):
name='添加个人日志'
text='点击确定打开添加页面'
messagebox.showinfo(name, text)
subprocess.run(["python", r"个人日志页面.py",username])
tjrz.bind('<Button-1>', ck6)
def ck5(event):
name='查看他人信息'
text='点击确定打开查看他人信息页面'
messagebox.showinfo(name, text)
subprocess.run(["python", r"查看他人信息页面.py"])
cktrxx.bind('<Button-1>', ck5)
root.mainloop()
4.查看个人页面:
from tkinter import *
from tkinter import Text
from tkinter import messagebox
import os
import sys
root=Tk()
root.title('个人信息')
root.geometry('250x200+700+300')
#查询传递的用户名:
if len(sys.argv)>1:
username = sys.argv[1]
else:
print("无用户信息")
def save_file():
content = xx_box.get("1.0", END) # 获取文本框内容
save_file_ = os.path.join(f"{username}_save.txt")
with open(save_file_, 'w', encoding='utf-8') as file_save:
file_save.write(content)
messagebox.showinfo("导出成功", f"内容已导出到 {save_file_}")
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.查看他人信息页面:
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"
rz_file = f"{u_name}_rz.txt"
rz_public_file = f"{u_name}_rz_public.txt"
# 检查文件是否存在,如果存在则删除
if os.path.exists(file):
os.remove(file)
if os.path.exists(file_public):
os.remove(file_public)
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")
# 写入 rz_file.txt 内容到 file
if os.path.exists(rz_file):
with open(rz_file, 'r', encoding='utf-8') as rz_read:
rz_f = rz_read.read()
with open(file, 'a', encoding='utf-8') as file_write:
file_write.write(rz_f)
# 写入 rz_public_file.txt 内容到 file_public
if os.path.exists(rz_public_file):
with open(rz_public_file, 'r', encoding='utf-8') as rz_public_read:
rz_public_f = rz_public_read.read()
with open(file_public, 'a', encoding='utf-8') as file_write_public:
file_write_public.write(rz_public_f)
name = ''
text = '修改成功!'
messagebox.showinfo(name, text)
root.destroy()
#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.管理员专用页面:
from tkinter import *
from tkinter import Text
from tkinter import messagebox
import os
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_rz=f"{delete_name}_rz.txt"
delete_name_file_self=f"{delete_name}_public.txt"
delete_public_rz = f"{delete_name}_rz_public.txt"
if os.path.exists(delete_name_file_public):
os.remove(delete_name_file_public)
os.remove(delete_rz)
else:
messagebox.showwarning("警告", f"用户 {delete_name} 不存在。")
if os.path.exists(delete_name_file_self):
os.remove(delete_name_file_self)
os.remove(delete_public_rz)
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()
8.个人日志页面:
from tkinter import *
from tkinter import Text
from tkinter import messagebox
import sys
root = Tk()
root.title('添加个人日志')
root.geometry('400x300+700+300')
# 查询传递的用户名:
if len(sys.argv) > 1:
username = sys.argv[1]
else:
username = "无"
print("无用户信息")
file = f"{username}.txt"
public_file = f"{username}_public.txt"
rz_file=f"{username}_rz.txt"
rz_public_file=f"{username}_rz_public.txt"
# 添加输入框和提示语
Label(root, text="请填写日志信息").pack()
y_year = Label(root, text="年:")
y_year.pack()
r_year = Entry(root)
r_year.pack()
y_month = Label(root, text="月:")
y_month.pack()
r_month = Entry(root)
r_month.pack()
y_day = Label(root, text="日:")
y_day.pack()
r_day = Entry(root)
r_day.pack()
Label(root, text="请输入你的日志").pack()
r_log = Text(root, height=5, width=40)
r_log.pack()
def yszh():
color = public.config('bg')[-1]
if color == 'pink':
# 还原
public.config(bg=public.master.cget('bg'))
else:
public.config(bg='pink') # 变成粉色
public = Button(root, text="公开", command=yszh)
public.pack()
# 提交按钮事件处理函数
def c1():
year = r_year.get()
month = r_month.get()
day = r_day.get()
log = r_log.get("1.0", END)
if year and month and day and log.strip(): # 检查输入是否为空
# 检查是否公开
if public.config('bg')[-1] == 'pink':
with open(public_file, "a", encoding='utf-8') as f:
f.write(f"{year}.{month}.{day}:\n{log}\n")
with open(rz_public_file, "a", encoding='utf-8') as f:
f.write(f"{year}.{month}.{day}:\n{log}\n")
messagebox.showinfo("成功", "日志已保存")
with open(file, "a", encoding='utf-8') as f:
f.write(f"{year}.{month}.{day}:\n{log}\n")
with open(rz_file, "a", encoding='utf-8') as f:
f.write(f"{year}.{month}.{day}:\n{log}\n")
messagebox.showinfo("成功", "日志已保存")
else:
messagebox.showwarning("警告", "请填写完整!")
tj = Button(root, text="提交", command=c1)
tj.pack()
root.mainloop()
部分实现截图: