"""Basic GUI BoilerPlate LICENSE AGREEMENT The Original Code is "gui-boilerplate.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 pprint class BoilerPlateWindow: def __init__(self, command): self.command = command self.root = Tk() self.root.title("BoilerPlate") # 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 # 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 freeze_dialog(self): # TODO: disable your input widgets 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.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!") # return 1 def call_business_layer(self): # TODO: Compose your options and call the call back command if # there is one. options = {} # no if self.command: self.command(self, options) def callback(root, options): import time print pprint.pformat(options) print "processing...", time.sleep(2) # pretend to process really long print "ok." if __name__=="__main__": window = BoilerPlateWindow(callback) window.start()