import os
import tkinter as tk
from tkinter import messagebox
import read
def read_specific_line():
# 实现读取指定行内容的函数
input_path = filename_entry.get()
filename = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
line_number = int(line_number_entry.get())
if '.txt' in filename:
# 执行相关操作
result = read.read_specific_line(filename, line_number)
# 在弹窗中显示结果
messagebox.showinfo("执行结果", result)
else:
filenames = os.listdir(filename)
results = []
for i in filenames:
if i.endswith('.txt'):
txt_name = filename + '/' + i
result = read.read_specific_line(txt_name, line_number)
output_str = f"{i} 第 {line_number} 行的内容:{result}\n"
results.append(output_str)
# 新建一个结果文件
result_file_name = filename + '/结果.txt'
read.create_empty_txt(result_file_name)
# 打开文件, 并以写入模式打开
f = open(result_file_name, 'w')
# 写入文本
for i in results:
f.write(str(i))
# 关闭文件
f.close()
def count_keyword_occurrences():
# 实现统计指定关键词数量的函数
input_path = filename_entry.get()
filename = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
keyword = keyword1_entry.get()
if '.txt' in filename:
# 执行相关操作
result = read.count_keyword_occurrences(filename, keyword)
# 在弹窗中显示结果
messagebox.showinfo("执行结果", result)
else:
filenames = os.listdir(filename)
results = []
for i in filenames:
if i.endswith('.txt'):
txt_name = filename + '/' + i
# 执行相关操作
result = read.count_keyword_occurrences(txt_name, keyword)
output_str = f"{i} 的 {keyword} 关键词数量:{result}\n"
results.append(output_str)
# 新建一个结果文件
result_file_name = filename + '/结果.txt'
read.create_empty_txt(result_file_name)
# 打开文件, 并以写入模式打开
f = open(result_file_name, 'w')
# 写入文本
for i in results:
f.write(str(i))
# 关闭文件
f.close()
def read_between_keywords():
# 实现读取两个指定关键词之间内容的函数
input_path = filename_entry.get()
filename = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
keyword1 = keyword1_entry.get()
keyword2 = keyword2_entry.get()
# 执行相关操作
if '.txt' in filename:
# 执行相关操作
result = read.read_between_keywords(filename, keyword1, keyword2)
# 在弹窗中显示结果
messagebox.showinfo("执行结果", result)
else:
filenames = os.listdir(filename)
results = []
for i in filenames:
if i.endswith('.txt'):
txt_name = filename + '/' + i
# 执行相关操作
result = read.read_between_keywords(txt_name , keyword1, keyword2)
output_str = f"{i} 的 {keyword1}和{keyword2} 关键词之间的内容:{result}\n"
results.append(output_str)
# 新建一个结果文件
result_file_name = filename + '/结果.txt'
read.create_empty_txt(result_file_name)
# 打开文件, 并以写入模式打开
f = open(result_file_name, 'w')
# 写入文本
for i in results:
f.write(str(i))
# 关闭文件
f.close()
def create_empty_txt():
# 实现创建空白 txt 的函数
input_path = filename_entry.get()
filename = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
# 执行相关操作
result = read.create_empty_txt(filename)
# 在弹窗中显示结果
messagebox.showinfo("执行结果", result)
def count_lines_and_characters():
# 统计行数和字符串数量 的函数
input_path = filename_entry.get()
filename = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
# 执行相关操作
if '.txt' in filename:
# 执行相关操作
result1, result2 = read.count_lines_and_characters(filename)
# 在弹窗中显示结果
messagebox.showinfo("执行结果", result1+result2)
else:
filenames = os.listdir(filename)
results = []
for i in filenames:
if i.endswith('.txt'):
txt_name = filename + '/' + i
# 执行相关操作
result1, result2 = read.count_lines_and_characters(txt_name)
output_str = f"{i} 的行数:{result1}和字数:{result2}\n"
results.append(output_str)
# 新建一个结果文件
result_file_name = filename + '/结果.txt'
read.create_empty_txt(result_file_name)
# 打开文件, 并以写入模式打开
f = open(result_file_name, 'w')
# 写入文本
for i in results:
f.write(str(i))
# 关闭文件
f.close()
def execute_function():
# 根据单选按钮的选择执行相应函数
selection = function_selection.get()
if selection == 1:
read_specific_line()
elif selection == 2:
count_keyword_occurrences()
elif selection == 3:
read_between_keywords()
elif selection == 4:
create_empty_txt()
elif selection == 5:
count_lines_and_characters()
# 创建界面窗口
window = tk.Tk()
# 单选按钮
function_selection = tk.IntVar()
read_specific_line_radio = tk.Radiobutton(window, text="读取指定行内容", variable=function_selection, value=1)
read_specific_line_radio.pack()
count_keyword_occurrences_radio = tk.Radiobutton(window, text="统计指定关键词数量", variable=function_selection,
value=2)
count_keyword_occurrences_radio.pack()
read_between_keywords_radio = tk.Radiobutton(window, text="读取指定关键词之间的内容", variable=function_selection,
value=3)
read_between_keywords_radio.pack()
create_empty_txt_radio = tk.Radiobutton(window, text="创建空白 txt", variable=function_selection, value=4)
create_empty_txt_radio.pack()
count_lines_and_characters_radio = tk.Radiobutton(window, text="统计行数和字符串数量", variable=function_selection, value=5)
count_lines_and_characters_radio.pack()
# 标签和文本框
filename_label = tk.Label(window, text="文件名:")
filename_label.pack()
filename_entry = tk.Entry(window)
filename_entry.pack()
keyword1_label = tk.Label(window, text="关键词1:")
keyword1_label.pack()
keyword1_entry = tk.Entry(window)
keyword1_entry.pack()
keyword2_label = tk.Label(window, text="关键词2:")
keyword2_label.pack()
keyword2_entry = tk.Entry(window)
keyword2_entry.pack()
line_number_label = tk.Label(window, text="行数:")
line_number_label.pack()
line_number_entry = tk.Entry(window)
line_number_entry.pack()
# 执行按钮
execute_button = tk.Button(window, text="执行", command=execute_function)
execute_button.pack()
window.mainloop()
parry学习记录
- 粉丝: 0
- 资源: 9
最新资源
- 毕设和企业适用springboot企业资源规划类及环境监控平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及酒店管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及旅游规划平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及食品配送管理平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及知识共享平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及智能图像识别系统源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及语音识别系统源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及信用评分平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及虚拟人类交互系统源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及信息管理系统源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及自动化控制系统源码+论文+视频.zip
- 毕设和企业适用springboot区块链技术类及工程管理平台源码+论文+视频.zip
- 毕设和企业适用springboot区块链技术类及城市智能运营平台源码+论文+视频.zip
- 毕设和企业适用springboot区块链技术类及民生服务平台源码+论文+视频.zip
- 毕设和企业适用springboot区块链技术类及供应链优化系统源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及远程医疗平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈