Python implements a simple text editor instance based on the Tkinter library

Source: Internet
Author: User
The example in this paper describes how Python implements a simple text editor based on the Tkinter library. Share to everyone for your reference. The implementation method is as follows:

# # {{http://code.activestate.com/recipes/578568/(r1) from Tkinter Import * from Tksimpledialog import Askstringfrom TkF Iledialog Import asksaveasfilenamefrom tkmessagebox Import Askokcancel class Quitter (Frame): Def __init_ _ (Self, Parent=none): frame.__init__ (self, parent) self.pack () widget = Button (self, text= ' Quit ', command=se Lf.quit) Widget.pack (Expand=yes, Fill=both, Side=left) def quit (self): ans = askokcancel (' Verify exit ', "really qui    T? ") If Ans:Frame.quit (self) class Scrolledtext (Frame): Def __init__ (self, Parent=none, text= ", File=none): frame.__init__ (self, parent) Self.pack (Expand=yes, Fill=both) self.makewidgets () self.settext (text, file) def MAKEWIDG ETS (self): Sbar = Scrollbar (self) Text = text (self, relief=sunken) sbar.config (Command=text.yview) te Xt.config (Yscrollcommand=sbar.set) sbar.pack (Side=right, fill=y) text.pack (Side=left, Expand=YES, fi Ll=both) seLf.text = text def settext (self, text= ", File=none): If File:text = open (file, ' R '). Read () Self.text.delete (' 1.0 ', END) Self.text.insert (' 1.0 ', text) self.text.mark_set (INSERT, ' 1.0 ') Self.text. Focus () def gettext (self): return Self.text.get (' 1.0 ', end+ ' -1c ') class Simpleedit    or (Scrolledtext): Def __init__ (self, Parent=none, file=none): frm = Frame (parent) frm.pack (fill=x) button (frm, text= ' Save ', Command=self.onsave). Pack (side=left) button (frm, text= ' Cut ', command=self.oncut). Pack (Side =left) button (frm, text= ' Paste ', command=self.onpaste). Pack (side=left) button (frm, text= ' Find ', command=self.onfind) . Pack (Side=left) quitter (frm). Pack (Side=left) scrolledtext.__init__ (self, parent, file=file) self.text.config (fo Nt= (' Courier ', 9, ' normal ')) def onSave (self): filename = asksaveasfilename () If Filename:alltext = Self.gett Ext () Open (FileName, ' W '). Write (Alltext) def oncut (self): Text = Self.text.get (Sel_first, Sel_last) Self.text.delete (                      Sel_first, Sel_last) self.clipboard_clear () self.clipboard_append (text) def onpaste (self): Try:text = Self.selection_get (selection= ' CLIPBOARD ') self.text.insert (insert, text) except Tclerro    R:pass def onfind (self): target = askstring (' simpleeditor ', ' Search String? ')        If Target:where = Self.text.search (target, INSERT, END) if Where:print where Pastit = where + (' +%DC '% len (target)) #self. Text.tag_remove (SEL, ' 1.0 ', END) Self.text.tag_add (SEL, W Here, Pastit) self.text.mark_set (INSERT, Pastit) self.text.see (insert) self.text.fo Cus () if __name__ = = ' __main__ ': Try:simpleeditor (File=sys.argv[1]). Mainloop () except Indexerror:si Mpleeditor (). Mainloop ()

Hopefully this article will help you with Python programming.

  • Related Article

    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.