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

Source: Internet
Author: User
Tags gettext pack

This article mainly introduces Python based on Tkinter Library to implement a simple text editor, example analysis of Python using Tkinter Library to achieve a simple desktop application skills, with a certain reference value, the need for friends can refer to the

This example describes Python's approach to implementing a simple text editor based on the Tkinter library. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 The 67 68 69 70 71 72 73 74 75 76 77 78 79 80 # # {{http://code.activestate.com/recipes/578568/(r1) from tkinter import * ' Tksimpledialog import askstring from T Kfiledialog Import asksaveasfilename from Tkmessagebox Import Askokcancel class Quitter (Frame): Def __init__ (self, parent =none): frame.__init__ (self, parent) self.pack () widget = Button (self, text= ' Quit ', command=self.quit) Widget.pack ( Expand=yes, Fill=both, Side=left) def quit (self): ans = askokcancel (' Verify exit ', "really quit?") 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 makewidgets (self): Sbar = Scrollbar (self) text = T Ext (self, relief=sunken) sbar.config (Command=text.yview) text.config (yscrollcommand=sbar.set) sbar.pack (Side=RIGHT , fill=y) Text.pack (Side=left, Expand=yes, fill=both) self.text = text def settext (self, text= ', file=none): If File:tex t = 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 Simpleeditor (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 (' Courier ', 9, ' normal ') def onSave (self): filename = asksaveasfilename () if fi Lename:alltext = Self.gettext () 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 Tclerror:pass def onfind (self): target = askstring (' simpleeditor ', ' Search String? ') if target:whe Re = Self.text.search (target, INSERT, end) if where:print where pastit = where + (' +%DC '% len (target) #self. text.tag_re Move (SEL, ' 1.0 ', end) Self.text.tag_add (SEL, where, pastit) self.text.mark_set (INSERT, Pastit) self.text.see (insert) Self.text.focus () if __name__ = = ' __main__ ': Try:simpleeditor (File=sys.argv[1]). Mainloop except () Indexerror: Simpleeditor (). Mainloop ()

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.