python写的一个文本编辑器
时间:2021-10-12 08:38:04
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#=============================================================================
# FileName:
# Desc:
# Author: ToughGuy
# Version: 0.0.1
# LastChange: 2013-02-20 14:52:11
# History:
#=============================================================================
from Tkinter import *
import tkMessageBox,tkFileDialog
import platform
# nl = os.linesep
def openfile():
global filename # 使用global声明为全局变量,方便后边的程序调用
systype = platform.system() # 判断系统类型
if systype == 'windows':
basedir = 'c:\\'
else:
basedir = '/'
filename = tkFileDialog.askopenfilename(initialdir=basedir)
try:
fobj_r = open(filename, 'r')
except IOError, errmsg:
print '*** Failed open file:', errmsg
else:
editbox.delete(1.0, END)
for eachline in fobj_r:
editbox.insert(INSERT, eachline)
fobj_r.close()
def savefile():
save_data = editbox.get(1.0, END)
try:
fobj_w = open(filename, 'w')
fobj_w.writelines(save_data.encode('utf-8'))
fobj_w.close()
tkMessageBox.showinfo(title='提示',
message='保存成功')
except IOError, errmsg:
tkMessageBox.showwarning(title='保存失败', message='保存出错 ')
tkMessageBox.showwarning(title='错误信息', message=errmsg)
except NameError:
tkMessageBox.showwarning(title='保存失败', message='未打开文件')
def showlinenum():
tkMessageBox.showinfo(title='提示',
message='这个功能作者现在不会写,放这里装饰用的.')
def destroy_ui(ui):
ui.destroy()
def aboutauthor():
author_ui = Toplevel()
author_ui.title('关于')
author_ui.geometry('200x80')
about_string = Label(author_ui,
text="作者: ToughGuy")
confirmbtn = Button(author_ui, text='确定',
command=lambda:destroy_ui(author_ui))
about_string.pack()
confirmbtn.pack()
# author_ui.mainloop()
def CreateMenus():
# 初始化菜单
Menubar = Menu(root)
# 创建文件菜单
filemenu = Menu(Menubar, tearoff=0)
filemenu.add_command(label='打开文件', command=openfile)
filemenu.add_command(label='保存文件', command=savefile)
filemenu.add_command(label='退出', command=lambda:destroy_ui(root))
Menubar.add_cascade(label='文件', menu=filemenu)
# 创建编辑菜单
editmenu = Menu(Menubar, tearoff=0)
editmenu.add_command(label='显示行号', command=showlinenum)
Menubar.add_cascade(label='编辑', menu=editmenu)
# 创建帮助菜单
helpmenu = Menu(Menubar, tearoff=0)
helpmenu.add_command(label='关于作者', command=aboutauthor)
Menubar.add_cascade(label='帮助', menu=helpmenu)
root.config(menu=Menubar)
root = Tk()
root.title('文本编辑器')
root.geometry('500x400')
CreateMenus()
editbox = Text(root, width=70, height=25, bg='white')
editbox.pack(side=TOP, fill=X)
root.mainloop()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow mnist 数据加载实现并画图效果
![](https://img.aspxhome.com/file/2023/3/90653_0s.png)
opencv基于Haar人脸检测和眼睛检测
![](https://img.aspxhome.com/file/2023/1/90891_0s.jpg)
使用pytorch加载并读取COCO数据集的详细操作
![](https://img.aspxhome.com/file/2023/0/82620_0s.png)
python优化数据预处理方法Pandas pipe详解
![](https://img.aspxhome.com/file/2023/7/83767_0s.png)
从Python的源码来解析Python下的freeblock
![](https://img.aspxhome.com/file/2023/7/61217_0s.png)
Flask之flask-script模块使用
python 中的命名空间,你真的了解吗?
Python实现判断一个字符串是否包含子串的方法总结
python查找与排序算法详解(示图+代码)
![](https://img.aspxhome.com/file/2023/1/77631_0s.png)
python如何生成密码字典
python opencv3实现人脸识别(windows)
由黄钻等级图标处理引发的思考
![](https://img.aspxhome.com/file/UploadPic/200911/16/20091116124056484s.jpg)
Pycharm学习教程(1) 定制外观
![](https://img.aspxhome.com/file/2023/4/77584_0s.jpg)
巧用Dreamweaver4文件库更新网站
![](https://img.aspxhome.com/file/UploadPic/20072/200723113133681s.gif)
Python中eval()函数的详细使用教程
![](https://img.aspxhome.com/file/2023/8/78578_0s.png)
python绘制BA无标度网络示例代码
![](https://img.aspxhome.com/file/2023/7/70817_0s.jpg)
asp日期转换成汉字格式程序
JavaScript中Infinity(无穷数)的使用和注意事项
智能录入表格[适合BS模式项目的录入页面]
Python 并行化执行详细解析
![](https://img.aspxhome.com/file/2023/0/70680_0s.png)