import patterns import mywidgets import time from PIL import Image def float_or_zero(x): try: return float(x) except: return 0.0 class UserInterface(object): def __init__(self, factory): self.input_a = factory.edit(text='0') self.input_b = factory.edit(text='0') self.result = factory.label(text='0') self.quit_button = factory.button(text='Quit') self.kill_gui_func = factory.do_quit factory.window().contents = [ Image.open('i-want-gnu.png'), factory.table(contents = [ [ 'this', self.input_a ], [ 'times this', self.input_b ], [ 'equals this', self.result ], ]), '\n', self.quit_button, ] class Facade(object): def __init__(self, gui): self.done = False self.gui = gui patterns.CallbackObserver(self.gui.input_a, self.input_updated) patterns.CallbackObserver(self.gui.input_b, self.input_updated) patterns.CallbackObserver(self.gui.quit_button, self.do_quit) def do_quit(self, subject): self.done = True self.gui.kill_gui_func() def input_updated(self, subject): a = float_or_zero(self.gui.input_a.text) b = float_or_zero(self.gui.input_b.text) self.gui.result.text = str(a * b) def main(): factory = mywidgets.TkWidgetFactory() gui = UserInterface(factory) facade = Facade(gui) while not facade.done: time.sleep(.2) if __name__ == '__main__': main()