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)
代码语言:javascript复制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'))
代码语言:javascript复制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 (' �' % 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)
代码语言:javascript复制def quit(self):
ans = askokcancel('Verify exit', "Really quit?")
if ans: Frame.quit(self)
if name == 'main': Quitter().mainloop()