python写的一个文本编辑器

yipeiwu_com5年前Python基础

复制代码 代码如下:

#!/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()

相关文章

Python从文件中读取指定的行以及在文件指定位置写入

Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据,  Python标准库linecache模块非常适合这个任务: 测试文件内容 : This...

python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例

这篇文章主要介绍了python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

Python利用pandas处理Excel数据的应用详解

Python利用pandas处理Excel数据的应用详解

最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做自动化测试的时候,如果涉及到数据的读取和存储...

python利用itertools生成密码字典并多线程撞库破解rar密码

脚本功能: 利用itertools生成密码字典(迭代器形式) 多线程并发从密码字典中取出密码进行验证 验证成功后把密码写入文件中保存 #!/usr/bin/env...

python多线程http压力测试脚本

本文实例为大家分享了python多线程http压力测试的具体代码,供大家参考,具体内容如下 #coding=utf-8 import sys import time import...