"""Example script that is based on gui-boilerplate.py LICENSE AGREEMENT The Original Code is "msr_report.py". The Initial Developer of the Original Code is Leica Geosystems. Portions created by Leica Geosystems are Copyright (C) 2014 year. Leica Geosystems. All Rights Reserved. """ from Tkinter import * import tkMessageBox import tkFileDialog import os import pprint from grail.data import geometry class MsrReportWindow: def __init__(self, command): self.command = command self.root = Tk() self.root.title("MSR Report") # bind in common events self.root.protocol("WM_DELETE_WINDOW", self.on_cancel) self.root.bind("", self.on_cancel) self.root.bind("", self.on_ok) # TODO: insert your widgets self.browse_frame = Frame(self.root) # above button frame self.browse_frame.pack(anchor='nw', side='top') self.label_path = Label(self.browse_frame, width=50, relief=RIDGE, justify=LEFT) self.btn_browse = Button(self.browse_frame, text="BROWSE...", command=self.on_browse) # we need to define command! self.label_path.grid(row=0, column=0, padx=2) self.btn_browse.grid(row=0, column=1, padx=2) # build the ok, apply cancel buttons self.buttonframe = Frame(self.root) self.buttonframe.pack(anchor='se', side='bottom') self.btn_ok = Button(self.buttonframe, text="OK", command=self.on_ok) self.btn_cancel = Button(self.buttonframe, text="CANCEL", command=self.on_cancel) self.btn_apply = Button(self.buttonframe, text="APPLY", command=self.on_apply) self.btn_cancel.pack(side='right', padx=2, pady=4) self.btn_apply.pack(side='right', padx=2, pady=4) self.btn_ok.pack(side='right', padx=2, pady=4) def start(self): self.root.mainloop() def on_cancel(self, event=None): self.root.destroy() def on_ok(self, event=None): self.freeze_dialog() if self.validate_input(): self.call_business_layer() self.unfreeze_dialog() self.root.destroy() def on_apply(self, event=None): self.freeze_dialog() if self.validate_input(): self.call_business_layer() self.unfreeze_dialog() def on_browse(self, event=None): types = [("Geometry MSR", "*.msr"), ("All Files", "*.*")] path = tkFileDialog.askopenfilename(parent=self.root, defaultextension=".msr", filetypes=types, title="Open MSR File") self.label_path.configure(text=path) def freeze_dialog(self): # TODO: disable your input widgets self.btn_browse.configure(state=DISABLED) self.oldcursor = self.root.cget("cursor") self.root.configure(cursor="watch") self.root.update() self.btn_ok.configure(state=DISABLED) self.btn_cancel.configure(state=DISABLED) self.btn_apply.configure(state=DISABLED) self.root.update_idletasks() def unfreeze_dialog(self): # TODO: enable your input widgets self.btn_browse.configure(state=ACTIVE) self.root.configure(cursor=self.oldcursor) self.root.update() self.btn_ok.configure(state=ACTIVE) self.btn_cancel.configure(state=ACTIVE) self.btn_apply.configure(state=ACTIVE) self.root.update_idletasks() def validate_input(self): # TODO: validate input for your widgets. Return 1 if everything # is valid; return 0 otherwise. Also post error messages from # here, like this, # # tkMessageBox.showerror("your path is invalid!") # isvalid = 0 path = self.label_path.cget("text") if not os.path.isfile(path): tkMessageBox.showerror(self.root, "Invalid path: %s" % (path)) isvalid = 0 elif not self._is_geometry_msr(path): tkMessageBox.showerror(self.root, "Path not pointing to Geometry MSR: %s" % (path)) isvalid = 0 else: isvalid = 1 # it must be valid by now. return isvalid def _is_geometry_msr(self, path): try: msr = geometry.Geometry(path) except geometry.GeometryError, e: return 0 # invalid return 1 # valid def call_business_layer(self): # TODO: Compose your options and call the call back command if # there is one. options = {"path":self.label_path.cget("text")} # valid path if self.command: output = self.command(self, options) tkMessageBox.showinfo(self.root, output) def report(root, options): # Options will be "path". path = options["path"] msr = geometry.Geometry(path) report = "TODO: generate report" # ...generate report ... return report if __name__=="__main__": window = MsrReportWindow(report) window.start()