当前位置:
文档之家› Python文本文档编辑器综合实验
Python文本文档编辑器综合实验
word_list[m]=word_list[m]+wenjian_list[j] m=m+1 if i+1!=n:
•
• • • • • •
if wenjian_list[i+1] not in whitespace:
a=i+1 elif i==n-1: word_list.append(wenjian_list[a]) for j in range(a+1,n): word_list[m]=word_list[m]+wenjian_list[j] return word_list #返回单词列表
按钮与判断按钮
• 1.button = Button(self.root, text="还待完善 ",command=self.hdws) • 2.Checkbutton(t1,variable = v,text = '显示详情',onvalue = 1, offvalue = 0, command lambda:self.details(v,t1)).pack() • v = IntVar() • def details(self,v,t1): if v.get()==0: expression = Text(t1, height = 20, width = 50) • expression.pack() expression.insert('1.end',self.word_list(self.yuanwenj ian())) •
另存为的实现(创新点)
•
self.file_address=input_bar.get('1.0','1.end')
• t1.destroy()
• if self.check_address()=='ok': • self.txt_save() 回到保存函数
重要的一些算法
• • • for j in range(n): a=wenjian_list[j] wenjian_list[j]=a.lower() #转换大小写
•
• • • • • • • • • • if wenjian_list[0] not in whitespace: a=0 for i in range(1,n): if wenjian_list[i] in whitespace: if wenjian_list[i-1] not in whitespace: word_list.append(wenjian_list[a]) for j in range(a+1,i): #将原文件列表中的单词录入纯文本列表 #遍历原文件列表
Python综合实验实验报告
——140517 14051166 肖昆
• • • • • • • • • • •
一大方法:类(创新点) 五大模块: 核心:tkinter matplotlib.pyplot numpy os(创新点) string(创新点) 创新功能实现 打开 保存,另存为 异常与处理
numpy
• 利用numpy这个三方库我们可以对函数的 处理主要是,它可以很方便地处理大型矩 阵和数组,并提供三角、对数、平方、立 方等各种基本函数
• plt.fien_number,explode=None,autopct=' %1.2f%%',labels=top_ten_name, colors=('b', 'g', 'r', 'c', 'm', 'y','k', 'w','g','r'),shadow=True,labeldistance=1.1) • 画饼状图 • plt.title('Key Words') 给饼状图标标题 • plt.show() 显示图
mesagebox messagebox.showinfo('我会加油的^_^','我会加油的^_^') 弹出消息框
grid 几何管理 bt.grid_configure(column = 3, row = 1, columnspan = 1, rowspan = 1)配置 t1.destroy()销毁窗口 self.expression.insert('1.end',self.yuanwenjian()) 插入 原文件
self.root.wm_resizable(width = False, height =False) 界面不可伸缩
Matplotlib
• pyplot • Matplotlib通过matplotlib.pyplot模块提供了 一套绘图API,将众多绘图对象所构成的复 杂结构隐藏在其内部。我们只需要调用 pyplot模块所提供的函数就可以实现快速绘 图以及设置图表的各种细节 • 这里,我利用它绘制饼状图
• t1= Toplevel(height = 200,width = 400)建立Toplevel窗口
菜单和子菜单的实现
• menubar = Menu(self.root) • filemenu = Menu(menubar, tearoff=0) • filemenu.add_command(label="新建", command=self.donothing) • editmenu.add_separator()加分割线 • menubar.add_cascade(label="文件", menu=filemenu)
string(创新点)
• whitespace=string.whitespace 空白字符 • +string.punctuation 标点字符 • a.lower() 转化为小写
打开的实现(创新点)
• wenjian=open(self.file_address,'r') • 打开原文件 • yuanwenjian=wenjian.read() 读取原文件 • wenjian.close() 关闭原文件 • self.expression.insert('1.end',self.yuanwenj ian()) 将原文件内容插入到txt处理器中
•
word_list_change.remove(a)
• • • • • • • •
for i in range(10): a=word_list_change[0] n=word_list_change.count(a) top_ten_name.append(a) top_ten_number.append(n) for j in range(n): word_list_change.remove(a) while word_list_change!=[]:
•
• • • • • • • • •
a=word_list_change[0]
n=word_list_change.count(a) if n>min(top_ten_number): x=top_ten_number.index(min(top_ten_number)) del top_ten_name[x] del top_ten_number[x] top_ten_name.insert(x,a) top_ten_number.insert(x,n) for j in range(n): word_list_change.remove(a)
• self.root=Tk()
创建顶层
• cv =Canvas(self.root,bg = 'white')
• cv.pack() 显示画布 • self.root.title('Txt处理器') 命名画布
创建画布
• self.expression = Text(cv, height = 20, width = 100) 加入 text框 • self.root.mainloop() • 让这个root工作起来,开始接收鼠标的和键盘的操作 • label1 = Label(t1,text = '总计单词数:') 贴标签
类(创新点)
• 优点 • 引入类是面对对象程序设计的重要特征, 方便计算机使用用人类的思维来处理事情 • 一个self是我的整个程序完美的衔接在了一 起,class真是个美妙的东西
tkinter
Tkinter模块定义了Python GUI设计所需 的各种控件,如Frame、Label、Button 、Text等 Tkinter模块中有一个主要的类为Widget (构件),Frame、Text、Button等都 是Widget类的子类
• 祝同学们期末考试考出理想成绩 • 祝老师们工作生活顺利
os(创新点)
• Python os模块包含普遍的操作系统功能
• 判断路径
• m,n=os.path.split(self.file_address)
• if os.path.isdir(m) and n!='':x=n[-4]+n[-3]+n[-2]+n[-1]
• • • • • if x=='.txt': print('address is true') return 'ok' else: print('address is wrong')
• • • • • •
while word_list_change!=[]: a=word_list_change[0] n=word_list_change.count(a) x=a+':'+str(n)+'\n' expression.insert(1.0,x) for i in range(n):