今天我们用python来做一个带有图形界面的文本编辑器,部分截图

类似微软的word的菜单栏

我们这个文本编辑器的功能是随便下载一段英文,可以统计词频,去掉一些类似is,am等常用词,转换大小写等功能,此外还能当作简单浏览器使用,可以打开特定网址

import string import re import matplotlib.pyplot as plt import numpy import tkinter def opencibiao(): stopfilename=open('停用词表.txt','r') stopfilelines=stopfilename.read() stopfilename.close() stop_list=stopfilelines.split('n') return stop_list #打开去词表 def openfile(): filename=open('101.txt','r') filelines=filename.read() filename.close() return filelines #打开下载的文本 def line_lists(filelines): line=re.sub('[,.]',' ',filelines) line=line.lower() line_list=line.split(' ') return line_list def cipin(line_list): stop_list=opencibiao() new_list=[] dict={} for word in line_list: if word not in stop_list: new_list.append(word) num=line_list.count(word) dict[word]=num tupledict=zip(dict.values(),dict.keys()) new_dict=sorted(tupledict) cc=new_dict[-6:] return cc #统计词频 def key_(cc): keys=(cc[0][1],cc[1][1],cc[2][1],cc[3][1],cc[4][1],cc[5][1]) return keys def value_(cc): values=(cc[0][0],cc[1][0],cc[2][0],cc[3][0],cc[4][0],cc[5][0]) return values def picture(keys,values): plt.figure(figsize=(8,6),dpi=80) plt.subplot(1,1,1) plt.xlabel('单词') plt.ylabel('个数') plt.title('词频表') index=numpy.arange(6) p2=plt.bar(index,values,0.35,label="rainfall",color="#87CEFA") plt.xticks(index,keys) plt.show() return #数据可视化 def on_click1(): print(opencibiao()) def on_click2(): a=opencibiao() b=openfile() c=line_lists(b) d=cipin(c) e=key_(d) f=value_(d) picture(e,f) def on_click3(): g=openfile() h=line_lists(g) print(len(h)) root=tkinter.Tk(className='文本编辑器') button1=tkinter.Button(root,text='打开文件',command=on_click1) button1.pack(side=tkinter.LEFT) button2=tkinter.Button(root,text='打开词表',command=on_click2) button2.pack(side=tkinter.RIGHT) button3=tkinter.Button(root,text='总词数',command=on_click3) button3.pack(side=tkinter.TOP) root.mainloop() #实现gui图形功能