Tkinter implementation of the text editor

Source: Internet
Author: User

Effect:

#-*-Encoding:utf8-*-#python 2.7from Tkinter import *from tkmessagebox import *from tkfiledialog import *import Osfilen Ame = "def author (): Showinfo (' Author: ', ' Sundy ') def about (): Showinfo (' Copyright: ', ' Sundy ') def openfile (): Globa        L filename filename = askopenfilename (defaultextension = '. txt ') if filename = = ': filename = None Else:        Root.title (' filename: ' +os.path.basename (filename)) Textpad.delete (1.0,end) f = open (filename, ' R ')    Textpad.insert (1.0,f.read ()) F.close () def new (): Global filename root.title (' unnamed file ') filename = None Textpad.delete (1.0,end) def Save (): global filename try:f = open (filename, ' w ') msg = Textpad.get (1.0 , END) f.write (msg) f.close () Except:saveas () def SaveAs (): F = asksaveasfilename (initialfile= '    Unnamed. txt ', defaultextension= '. txt ') global filename filename = f fh = open (f, ' w ') msg = Textpad.get (1.0,end) Fh.write (msg) Fh.cloSE () root.title (' FileName: ' +os.path.basename (f)) def cut (): Textpad.event_generate (' <<Cut>> ') def copy () : Textpad.event_generate (' <<Copy>> ') def paste (): Textpad.event_generate (' <<Paste>> ') def Redo (): textpad.event_generate (' <<Redo>> ') def undo (): Textpad.event_generate (' <<Undo>> ')             Def selectall (): Textpad.tag_add (' sel ', ' 1.0 ', END) def search (): Def dosearch (): Myentry = Entry1.get () #获取查找的内容--string Type whatever = str (Textpad.get (1.0,end)) # Print Textpad.index (' zxc ') # print Myen Try # print "%d"% (Whatever.count (myentry)) #计算substr在S中出现的次数 showinfo ("Find results:", "You searched%s, there ar E%d in the text "% (Myentry,whatever.count (myentry))) # Print Whatever.find (myentry) # Teindex = Textpad.inde X (myentry) # Textpad.linestart (teindex) # textpad.mark_set (' Insert ', Teindex) # Textpad.mark_set (Myen Try,current + ' +5c ') # textPad.mark_set (myentry,current + ' wordstart ') Topsearch = toplevel (root) topsearch.geometry (' 300x30+200+250 ') Labe L1 = Label (topsearch,text= ' Find ') Label1.grid (row=0, column=0,padx=5) Entry1 = Entry (topsearch,width=20) entry1.g    RID (row=0, column=1,padx=5) button1 = Button (topsearch,text= ' find ', Command=dosearch) Button1.grid (row=0, column=2) root = Tk () root.title (' Sundy Node ') root.geometry ("800x500+100+100") #Create Menumenubar = menu (Root) root.config (menu = MenuBar) Filemenu = Menu (menubar) filemenu.add_command (label= ' new ', accelerator= ' Ctrl + N ', command= new) filemenu.add_ Command (label= ' open ', accelerator= ' Ctrl + O ', command = OpenFile) Filemenu.add_command (label= ' save ', accelerator= ' Ctrl + S ', Command=save) Filemenu.add_command (label= ' Save As ', accelerator= ' Ctrl + Shift + S ', Command=saveas) Menubar.add_cascade ( Label= ' file ', menu=filemenu) Editmenu = menu (menubar) editmenu.add_command (label= ' undo ', Accelerator= ' Ctrl + Z ', command= undo) Editmenu.add_command (label= ' Redo ', accelerator= ' Ctrl + y ', Command=redo) Editmenu.add_separator () editmenu.add_command (label = "Cut", accelerator = "Ctrl + X", command=cut) Editmenu.add_command (label = "Copy", accelerator = "Ctrl + C", command=copy) editmenu.add_command (label = "Paste", accelerator = "Ctrl + V", command= paste) editmenu.add_separator () editmenu.add_command (label = "Find", accelerator = "Ctrl + F", Command=se Arch) editmenu.add_command (label = "Select All", accelerator = "Ctrl + A", command= SelectAll) menubar.add_cascade (label = "Action", menu = editmenu) Aboutmenu = menu (menubar) aboutmenu.add_command (label = "Author", Command=author) Aboutmenu.add_command ( label = "about", command = ON) menubar.add_cascade (label = "About", menu=aboutmenu) #toolbartoolbar = Frame (Root, height=25 , bg= ' grey ') Shortbutton = Button (toolbar, text= ' open ', command = OpenFile) shortbutton.pack (Side=left, padx=5, pady=5) Shortbutton = Button (toolbar, text= ' save ', command = Save) shortbutton.pack (Side=left) Toolbar.pack (expand=no,fill=x) # Status Barstatus = Label (root, text= ' Ln20 ', bd=1, Relief=sunken,anCHOR=W) Status.pack (Side=bottom, fill=x) #linenumber &textlnlabel =label (Root, width=2, bg= ' antique white ') Lnlabel.pack (Side=left, fill=y) TextPad = Text (root, Undo=true) textpad.pack (Expand=yes, fill=both) scroll = Scrollbar ( TextPad) Textpad.config (yscrollcommand= scroll.set) scroll.config (command = Textpad.yview) Scroll.pack (Side=RIGHT, fill=y) Root.mainloop ()

Recommended Documents: http://effbot.org/tkinterbook/

*python2.7 Environment: In Python3. Tkinter in x has changed, please note!!

Tkinter implementation of the text editor

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.