三、文本统计器 要求: 1、设计一个窗口,该窗口中有五个文本区。 2、五个文本区中一个可供用户编辑文本,另外四个不可编辑。 3、当用户在可编辑的文本区中进行文本编辑操作时,另外四个不可编辑的文本区中,一个实时显示用户所编辑文本中所出现的单词,一个显示用户所编辑文本中所出现的单词总数,一个实时显示用户所编辑文本中出现的数字,一个显示用户所编辑文本中出现的数字的总数。 4、系统具备良好的可扩展性,以便后期可添加统计中文汉字、标点符号的功能。
时间: 2024-02-28 14:54:36 浏览: 99
这是一道编程题,需要使用某种编程语言进行实现。下面是一个 Python 3 的示例实现,使用了 tkinter 库来构建窗口和文本区,使用正则表达式来匹配单词和数字。
```python
import tkinter as tk
import re
class TextStats:
def __init__(self, master):
self.master = master
self.master.title("文本统计器")
self.master.geometry("800x600")
# 创建五个文本区
self.edit_text = tk.Text(master, wrap="word")
self.edit_text.pack(side="left", fill="both", expand=True)
self.word_text = tk.Text(master, state="disabled")
self.word_text.pack(side="right", fill="both", expand=True)
self.word_count_text = tk.Text(master, state="disabled")
self.word_count_text.pack(side="right", fill="both", expand=True)
self.num_text = tk.Text(master, state="disabled")
self.num_text.pack(side="right", fill="both", expand=True)
self.num_count_text = tk.Text(master, state="disabled")
self.num_count_text.pack(side="right", fill="both", expand=True)
# 绑定事件处理函数
self.edit_text.bind("<KeyRelease>", self.update_stats)
# 初始化统计信息
self.word_count = 0
self.num_count = 0
self.word_dict = {}
def update_stats(self, event):
# 清空统计信息
self.word_text.config(state="normal")
self.word_text.delete("1.0", "end")
self.word_count_text.config(state="normal")
self.word_count_text.delete("1.0", "end")
self.num_text.config(state="normal")
self.num_text.delete("1.0", "end")
self.num_count_text.config(state="normal")
self.num_count_text.delete("1.0", "end")
self.word_dict = {}
self.word_count = 0
self.num_count = 0
# 获取编辑区的文本
text = self.edit_text.get("1.0", "end")
# 匹配单词
words = re.findall(r"\b\w+\b", text)
for word in words:
self.word_count += 1
if word in self.word_dict:
self.word_dict[word] += 1
else:
self.word_dict[word] = 1
# 显示单词列表和单词总数
self.word_text.insert("1.0", "\n".join(sorted(self.word_dict.keys())))
self.word_count_text.insert("1.0", str(self.word_count))
# 匹配数字
nums = re.findall(r"\b\d+\b", text)
for num in nums:
self.num_count += 1
# 显示数字列表和数字总数
self.num_text.insert("1.0", "\n".join(nums))
self.num_count_text.insert("1.0", str(self.num_count))
# 禁用不可编辑的文本区
self.word_text.config(state="disabled")
self.word_count_text.config(state="disabled")
self.num_text.config(state="disabled")
self.num_count_text.config(state="disabled")
root = tk.Tk()
app = TextStats(root)
root.mainloop()
```
该程序使用了面向对象的编程风格,将窗口和文本区的创建、事件处理函数的绑定、统计信息的更新等功能封装在一个 TextStats 类中。在 update_stats 函数中,使用正则表达式分别匹配单词和数字,并统计它们的数量和出现次数,最终更新四个不可编辑的文本区。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)