[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Example 10-2. PP4E\Gui\Tools\guimixin.py


"""
###############################################################################
a "mixin" class for other frames: common methods for canned dialogs,
spawning programs, simple text viewers, etc; this class must be mixed
with a Frame (or a subclass derived from Frame) for its quit method
###############################################################################
"""


from tkinter import
from tkinter.messagebox import

from tkinter.filedialog import *
from PP4E.Gui.Tour.scrolledtext import ScrolledText # or tkinter.scrolledtext
from PP4E.launchmodes import PortableLauncher, System # or use multiprocessing


class GuiMixin:
def infobox(self, title, text, args): # use standard dialogs
return showinfo(title, text) #
args for bkwd compat


def errorbox(self, text):
showerror('Error!', text)


def question(self, title, text, *args):
return askyesno(title, text) # return True or False


def notdone(self):
showerror('Not implemented', 'Option not available')


def quit(self):
ans = self.question('Verify quit', 'Are you sure you want to quit?')
if ans:
Frame.quit(self) # quit not recursive!


def help(self):
self.infobox('RTFM', 'See figure 1...') # override this better


def selectOpenFile(self, file="", dir="."): # use standard dialogs
return askopenfilename(initialdir=dir, initialfile=file)


def selectSaveFile(self, file="", dir="."):
return asksaveasfilename(initialfile=file, initialdir=dir)


def clone(self, args=()): # optional constructor args
new = Toplevel() # make new in-process version of me
myclass = self.class # instance's (lowest) class object
myclass(new, *args) # attach/run instance to new window


def spawn(self, pycmdline, wait=False):
if not wait: # start new process
PortableLauncher(pycmdline, pycmdline)() # run Python progam
else:
System(pycmdline, pycmdline)() # wait for it to exit


def browser(self, filename):
new = Toplevel() # make new window


600 | Chapter 10: GUI Coding Techniques

Free download pdf