python写的一个文本编辑器
admin
2023-07-31 02:00:40
0

复制代码 代码如下:
#!/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(\’200×80\’)
    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(\’500×400\’)
CreateMenus()
editbox = Text(root, width=70, height=25, bg=\’white\’)
editbox.pack(side=TOP, fill=X)
root.mainloop()

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...