view = ScrolledText(new, file=filename) # Text with Scrollbar
view.text.config(height=30, width=85) # config Text in Frame
view.text.config(font=('courier', 10, 'normal')) # use fixed-width font
new.title("Text Viewer") # set window mgr attrs
new.iconname("browser") # file text added auto
"""
def browser(self, filename): # if tkinter.scrolledtext
new = Toplevel() # included for reference
text = ScrolledText(new, height=30, width=85)
text.config(font=('courier', 10, 'normal'))
text.pack(expand=YES, fill=BOTH)
new.title("Text Viewer")
new.iconname("browser")
text.insert('0.0', open(filename, 'r').read() )
"""
if name == 'main':
class TestMixin(GuiMixin, Frame): # standalone test
def init(self, parent=None):
Frame.init(self, parent)
self.pack()
Button(self, text='quit', command=self.quit).pack(fill=X)
Button(self, text='help', command=self.help).pack(fill=X)
Button(self, text='clone', command=self.clone).pack(fill=X)
Button(self, text='spawn', command=self.other).pack(fill=X)
def other(self):
self.spawn('guimixin.py') # spawn self as separate process
TestMixin().mainloop()
Although Example 10-2 is geared toward GUIs, it’s really about design concepts. The
GuiMixin class implements common operations with standard interfaces that are im-
mune to changes in implementation. In fact, the implementations of some of this class’s
methods did change—between the first and second editions of this book, old-style
Dialog calls were replaced with the new Tk standard dialog calls; in the fourth edition,
the file browser was updated to use a different scrolled text class. Because this class’s
interface hides such details, its clients did not have to be changed to use the new
techniques.
As is, GuiMixin provides methods for common dialogs, window cloning, program
spawning, text file browsing, and so on. We can add more methods to such a mixin
later if we find ourselves coding the same methods repeatedly; they will all become
available immediately everywhere this class is imported and mixed. Moreover, Gui
Mixin’s methods can be inherited and used as is, or they can be redefined in subclasses.
Such are the natural advantages of classes over functions.
GuiMixin: Common Tool Mixin Classes | 601