[Python] 纯文本查看 复制代码
import os
import re
import tkinter as tk
from tkinter import messagebox, scrolledtext
from tkinter import filedialog
from wxauto import WeChat
config_dir = os.path.join(os.getcwd(), 'config')
custom_message_path = os.path.join(config_dir, 'custom_wechat_message.txt')
def create_file_if_not_exists(directory, filename):
file_path = os.path.join(directory, filename)
if not os.path.exists(file_path):
with open(file_path, 'w', encoding='utf-8') as f:
pass
def extract_names(text):
pattern = re.compile(r'[\u4e00-\u9fff]{1,4}')
return set(pattern.findall(text))
def open_file(filename):
try:
os.startfile(filename)
except Exception as e:
messagebox.showerror("错误", f"无法打开文件: {e}")
def show_description():
description = (
"班级名单\n"
" 填写方式:“=”前为真名,“=”后为微信名。\n"
" 示例:\n"
" 冯子洋=@冯子洋(13647692406) √\n"
" 冯军杰=冯军杰187852309\n\n"
"微信班级群名\n"
" 填写微信群名,用于发送信息,请确保微信已登录。\n"
" 示例:23计算机网络技术班\n\n"
"已报名单\n"
" 只能填写真名,且一行一个名字。\n\n"
"自定义微信信息\n"
" 可以自定义@后边的信息。\n"
)
desc_window = tk.Toplevel()
desc_window.title("说明")
text_area = scrolledtext.ScrolledText(desc_window, wrap=tk.WORD, width=60, height=20)
text_area.insert(tk.INSERT, description)
text_area.pack(padx=10, pady=10)
text_area.config(state='disabled')
def open_settings():
settings_window = tk.Toplevel(root)
settings_window.title("设置")
settings_window.geometry("400x300")
buttons = [
("班级名单", "班级名单.txt"),
("已报名单", "已报名单.txt"),
("微信班级群名", "微信班级群名.txt"),
("自定义微信信息", "custom_wechat_message.txt"),
("说明", None)
]
for btn_text, file_name in buttons:
btn = tk.Button(settings_window, text=btn_text, width=20, command=lambda f=file_name: open_file_button(f))
btn.pack(pady=5)
def open_file_button(file_name):
if file_name:
file_path = os.path.join(config_dir, file_name)
if not os.path.exists(file_path):
create_file_if_not_exists(config_dir, file_name)
open_file(file_path)
else:
show_description()
def show_about():
about_window = tk.Toplevel(root)
about_window.title("关于")
about_window.geometry("400x150")
# 创建标签并设置字体颜色为蓝色以模拟链接样式
link_label = tk.Label(about_window, text="吾爱破解论坛", fg="blue", cursor="hand2", font=("Arial", 14, "underline"))
link_label.pack(pady=20)
# 定义点击链接时打开网页的函数
def open_link(event):
import webbrowser
webbrowser.open("https://www.52pojie.cn/")
# 绑定鼠标点击事件到标签
link_label.bind("<Button-1>", open_link)
def check_unenrolled():
class_list_path = os.path.join(config_dir, '班级名单.txt')
enrolled_list_path = os.path.join(config_dir, '已报名单.txt')
# 读取班级名单
class_names = {}
with open(class_list_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
if '=' in line:
real_name, wechat_name = line.split('=', 1)
real_name = real_name.strip()
wechat_name = wechat_name.strip()
class_names[real_name] = wechat_name
else:
real_name = line.strip()
class_names[real_name] = real_name # 默认微信名与真名相同
# 读取已报名单
enrolled_names = set()
with open(enrolled_list_path, 'r', encoding='utf-8') as f:
for line in f:
extracted = extract_names(line)
enrolled_names.update(extracted)
# 计算未报名名单
not_enrolled = set(class_names.keys()) - enrolled_names
total = len(class_names)
enrolled = len(enrolled_names)
not_enrolled_count = len(not_enrolled)
# 显示统计信息
stats = f"总人数:{total}\n已报人数:{enrolled}\n未报人数:{not_enrolled_count}"
messagebox.showinfo("统计信息", stats)
# 显示未报名名单
if not not_enrolled:
messagebox.showinfo("结果", "所有学生都已报名。")
return
# 每行显示7个名字
not_enrolled_text = "\n".join([", ".join(sorted(not_enrolled)[i:i+7]) for i in range(0, len(sorted(not_enrolled)), 7)])
messagebox.showinfo("未报名名单", not_enrolled_text)
def send_reminder():
# 读取自定义微信信息
if not os.path.exists(custom_message_path):
create_file_if_not_exists(config_dir, 'custom_wechat_message.txt')
custom_message = ",你们家长还没有反馈你们到家情况。"
else:
with open(custom_message_path, 'r', encoding='utf-8') as f:
custom_message = f.read().strip()
# 读取班级名单和已报名单
class_list_path = os.path.join(config_dir, '班级名单.txt')
enrolled_list_path = os.path.join(config_dir, '已报名单.txt')
# 读取班级名单
class_names = {}
with open(class_list_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
if '=' in line:
real_name, wechat_name = line.split('=', 1)
real_name = real_name.strip()
wechat_name = wechat_name.strip()
class_names[real_name] = wechat_name
else:
real_name = line.strip()
class_names[real_name] = real_name # 默认微信名与真名相同
# 读取已报名单
enrolled_names = set()
with open(enrolled_list_path, 'r', encoding='utf-8') as f:
for line in f:
extracted = extract_names(line)
enrolled_names.update(extracted)
# 计算未报名名单
not_enrolled = set(class_names.keys()) - enrolled_names
if not not_enrolled:
messagebox.showinfo("结果", "所有学生都已报名。")
return
# 构建消息内容
message = ""
for name in sorted(not_enrolled):
wechat_name = class_names.get(name, name) # 如果没有微信名,则使用真名
message += f"@{wechat_name} "
message += custom_message
# 读取微信班级群名
contact_list_path = os.path.join(config_dir, '微信班级群名.txt')
with open(contact_list_path, 'r', encoding='utf-8') as f:
contact_name = f.read().strip()
if not contact_name:
messagebox.showerror("错误", "微信班级群名.txt 中没有联系人信息!")
return
# 发送消息
try:
wx = WeChat()
wx.SendMsg(message, contact_name)
messagebox.showinfo("结果", "消息已发送。")
except Exception as e:
messagebox.showerror("错误", f"发送消息失败: {e}")
root = tk.Tk()
root.title("检查程序")
root.geometry("400x350")
label = tk.Label(root, text="检查程序", font=("Arial", 16))
label.pack(pady=10)
frame = tk.Frame(root)
frame.pack(pady=10)
settings_btn = tk.Button(frame, text="设置", width=10, command=open_settings)
settings_btn.pack(side=tk.LEFT, padx=10)
about_btn = tk.Button(frame, text="关于", width=10, command=show_about)
about_btn.pack(side=tk.RIGHT, padx=10)
unenrolled_label = tk.Label(root, text="未报名单:", font=("Arial", 12))
unenrolled_label.pack(pady=5)
check_btn = tk.Button(root, text="检查", width=10, command=check_unenrolled)
check_btn.pack(pady=5)
remind_btn = tk.Button(root, text="提醒", width=10, command=send_reminder)
remind_btn.pack(pady=5)
root.mainloop()