python 簡易編輯器 text

image.png

"a simple text or file viewer component"

print('PP4E scrolledtext')
from tkinter import *

class ScrolledText(Frame):
def init(self, parent=None, text='', file=None):
Frame.init(self, parent)
self.pack(expand=YES, fill=BOTH) # make me expandable
self.makewidgets()
self.settext(text, file)

def makewidgets(self):
    sbar = Scrollbar(self)
    text = Text(self, relief=SUNKEN)
    sbar.config(command=text.yview)                  # xlink sbar and text
    text.config(yscrollcommand=sbar.set)             # move one moves other
    sbar.pack(side=RIGHT, fill=Y)                    # pack first=clip last
    text.pack(side=LEFT, expand=YES, fill=BOTH)      # text clipped first
    self.text = text

def settext(self, text='', file=None):
    if file:
        text = open(file, 'r').read()
    self.text.delete('1.0', END)                     # delete current text
    self.text.insert('1.0', text)                    # add at line 1, col 0
    self.text.mark_set(INSERT, '1.0')                # set insert cursor
    self.text.focus()                                # save user a click

def gettext(self):                                   # returns a string
    return self.text.get('1.0', END+'-1c')           # first through last

"""
add common edit tools to ScrolledText by inheritance;
composition (embedding) would work just as well here;
this is not robust!--see PyEdit for a feature superset;
"""

from tkinter import *
from tkinter.simpledialog import askstring
from tkinter.filedialog import asksaveasfilename
from quitter import Quitter

class SimpleEditor(ScrolledText): # see PyEdit for more
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(font=('courier', 9, 'normal'))

def onSave(self):
    filename = asksaveasfilename()
    if filename:
        alltext = self.gettext()                      # first through last
        open(filename, 'w').write(alltext)            # store text in file

def onCut(self):
    text = self.text.get(SEL_FIRST, SEL_LAST)         # error if no select
    self.text.delete(SEL_FIRST, SEL_LAST)             # should wrap in try
    self.clipboard_clear()
    self.clipboard_append(text)

def onPaste(self):                                    # add clipboard text
    try:
        text = self.selection_get(selection='CLIPBOARD')
        self.text.insert(INSERT, text)
    except TclError:
        pass                                          # not to be pasted

def onFind(self):
    target = askstring('SimpleEditor', 'Search String?')
    if target:
        where = self.text.search(target, INSERT, END)  # from insert cursor
        if where:                                      # returns an index
            print(where)
            pastit = where + ('+%dc' % len(target))    # index past target
           #self.text.tag_remove(SEL, '1.0', END)      # remove selection
            self.text.tag_add(SEL, where, pastit)      # select found target
            self.text.mark_set(INSERT, pastit)         # set insert mark
            self.text.see(INSERT)                      # scroll display
            self.text.focus()                          # select text widget

if name == 'main':
if len(sys.argv) > 1:
SimpleEditor(file=sys.argv[1]).mainloop() # filename on command line
else:
SimpleEditor().mainloop() # or not: start empty


quitter.py

"""
a Quit button that verifies exit requests;
to reuse, attach an instance to other GUIs, and re-pack as desired
"""

from tkinter import * # get widget classes
from tkinter.messagebox import askokcancel # get canned std dialog

class Quitter(Frame): # subclass our GUI
def init(self, parent=None): # constructor method
Frame.init(self, parent)
self.pack()
widget = Button(self, text='Quit', command=self.quit)
widget.pack(side=LEFT, expand=YES, fill=BOTH)

def quit(self):
    ans = askokcancel('Verify exit', "Really quit?")
    if ans: Frame.quit(self)

if name == 'main': Quitter().mainloop()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • tkinter ,wxPython,pyQT 1、from tkinter import Labelwidget=...
    SkTj閱讀 1,023評論 0 1
  • 本文主要講述官方提供的客戶端以及自己寫的增刪查改工具: ros_tool.py功能總匯,展示界面用了python的...
    追尋823閱讀 3,486評論 0 1
  • 75 Tkinter GUI初體驗(yàn) import tkinter as tk class APP: def __i...
    楊大菲閱讀 499評論 0 0
  • 1.35.3 | 為什么人人都要?dú)⒗畎祝?其他詩人首先是社會(huì)人,其次才是詩人,李白正好相反,首先是詩人,其次才是社...
    Pheeb閱讀 264評論 0 0
  • 一、生活 1、慢才是快。之前總是很焦慮,覺得時(shí)間過得太過,自己長進(jìn)太慢。減肥、學(xué)習(xí)、晉升、理財(cái),凡此種種,多數(shù)人往...
    Echo雪衣公子閱讀 495評論 0 3

友情鏈接更多精彩內(nèi)容