A text editor written by Python

Source: Internet
Author: User
Tags save file
The code is as follows:


#!/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 # is declared as a global variable using global, which makes it easy for the program to call
Systype = Platform.system () # To determine the system type
if Systype = = ' windows ':
Basedir = ' c:\\ '
Else:
Basedir = '/'
filename = tkfiledialog.askopenfilename (initialdir=bas Edir)
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 () /p>

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= ' hint ',
Message= ' Save succeeded ')
Except IOError, ErrMsg:
Tkmessagebox.showwarning (title= ' save failed ', message= ' Save error ')
Tkmessagebox.showwarning (title= ' error message ', message=errmsg)
Except Nameerror:
Tkmessagebox.showwarning (title= ' save failed ', message= ' not open file ')
Def showlinenum ():
Tkmessagebox.showinfo (title= ' hint ',
Message= ' This feature author is not going to write now, put it here for decorating. ')
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= "Author: Toughguy")
CONFIRMBTN = Button (author_ui, text= ' OK ',
COMMAND=LAMBDA:DESTROY_UI (AUTHOR_UI))
About_string.pack ()
Confirmbtn.pack ()
# Author_ui.mainloop ()

Def createmenus ():
# Initialize Menu
Menubar = Menu (Root)

# Create File menu
Filemenu = Menu (Menubar, tearoff=0)
Filemenu.add_command (label= ' Open file ', command=openfile)
Filemenu.add_command (label= ' Save file ', Command=savefile)
Filemenu.add_command (label= ' exit ', command=lambda:destroy_ui (root))
Menubar.add_cascade (label= ' file ', Menu=filemenu)

# Create an Edit menu
Editmenu = Menu (Menubar, tearoff=0)
Editmenu.add_command (label= ' show line number ', Command=showlinenum)
Menubar.add_cascade (label= ' 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 ()

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.