拿tkinter编写一个小的工具
import tkinter as tk
from tkinter import filedialog, messagebox
def check_content(file_path, choice):
try:
# 检查文件扩展名
# if not file_path.lower().endswith('.txt'):
# raise ValueError("文件格式不正确,请选择文本文件(.txt)")
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
line_count = len(lines)
content = ''.join(lines)
# 判断内容是否包含单选框选择的值
if choice in content:
result = "文件包含: {}".format(choice)
else:
result = "文件不包含: {}".format(choice)
# 更新日志信息
log_text.insert(tk.END, "读取到文件: {}\n".format(file_path))
log_text.insert(tk.END, "文件行数: {}\n".format(line_count))
log_text.insert(tk.END, "比对完成: {}\n".format(result))
# 弹出提示框
except Exception as e:
messagebox.showerror("错误", str(e))
def process_file():
# 获取输入的文件路径
file_path = entry_file_path.get()
# 获取单选框的选择
choice = var.get()
# 检查文件内容
check_content(file_path, choice)
# 创建主窗口
root = tk.Tk()
root.title("文件检查程序")
root.geometry("600x400")
# 创建一个Label用于提示用户输入文件路径
label_file_path = tk.Label(root, text="请输入文件路径:")
label_file_path.pack(pady=10)
# 创建一个文本输入框
entry_file_path = tk.Entry(root, width=50)
entry_file_path.pack(pady=5)
# 创建一个单选框的变量,默认选择号码
var = tk.StringVar(value="号码")
# 创建单选框,并绑定变量
radio_number = tk.Radiobutton(root, text="号码", variable=var, value="号码")
radio_id = tk.Radiobutton(root, text="身份证", variable=var, value="身份证")
radio_number.pack(pady=5)
radio_id.pack(pady=5)
# 创建一个按钮,点击后执行process_file函数
button_process = tk.Button(root, text="检查文件", command=process_file)
button_process.pack(pady=20)
# 创建一个多行文本框,用于显示日志信息
log_text = tk.Text(root, height=10, width=60)
log_text.pack(pady=10)
# 进入主循环
root.mainloop()