A text editor written in python

  • 2020-04-02 13:21:45
  • OfStack


#!/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             #  use global Declare as a global variable to facilitate subsequent program calls 
    systype = platform.system() #  Type of judgment 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=' prompt ',
                message=' Save success ')
    except IOError, errmsg:
        tkMessageBox.showwarning(title=' Save failed ', message=' Save the error     ')
        tkMessageBox.showwarning(title=' The error message ', message=errmsg)
    except NameError:
        tkMessageBox.showwarning(title=' Save failed ', message=' Open file ')
def showlinenum():
    tkMessageBox.showinfo(title=' prompt ',
            message=' The author of this feature will not write it now , It's here for decoration .')
def destroy_ui(ui):
    ui.destroy()
def aboutauthor():
    author_ui = Toplevel()
    author_ui.title(' about ')
    author_ui.geometry('200x80')
    about_string = Label(author_ui,
            text=" The author : ToughGuy")
    confirmbtn = Button(author_ui, text=' determine ',
            command=lambda:destroy_ui(author_ui))
    about_string.pack()
    confirmbtn.pack()
    # author_ui.mainloop()
def CreateMenus():
    #  Initialization menu 
    Menubar = Menu(root)
    #  Create file menu 
    filemenu = Menu(Menubar, tearoff=0)
    filemenu.add_command(label=' Open the file ', command=openfile)
    filemenu.add_command(label=' Save the file ', command=savefile)
    filemenu.add_command(label=' exit ', command=lambda:destroy_ui(root))
    Menubar.add_cascade(label=' file ', menu=filemenu)
    #  Create edit menu 
    editmenu = Menu(Menubar, tearoff=0)
    editmenu.add_command(label=' According to the line Numbers ', command=showlinenum)
    Menubar.add_cascade(label=' The editor ', menu=editmenu)
    #  Create help menu 
    helpmenu = Menu(Menubar, tearoff=0)
    helpmenu.add_command(label=' About the author ', command=aboutauthor)
    Menubar.add_cascade(label=' help ', menu=helpmenu)
    root.config(menu=Menubar)
root = Tk()
root.title(' Text editor ')
root.geometry('500x400')
CreateMenus()
editbox = Text(root, width=70, height=25, bg='white')
editbox.pack(side=TOP, fill=X)
root.mainloop()


Related articles: