#! / usr / bin / env python # coding: utf-8 # vim: ai ts = 4 sts = 4 et sw = 4 import gtk import os class EntryDescriptor (object): def __init __ (self, object_name): self.object_name = object_name def __get __ (self, obj, cls): return obj.get_object (self.object_name) .get_text () def __set __ (self, obj, value): obj.get_object (self.object_name) .set_text (value) class Calculator (gtk.Builder): def __init __ (self): super (Calculator, self) .__ init __ () # load demo.ui self.add_from_file (os.path.join (os.path.dirname (__ file __), 'demo.ui')) # connect the signal handlers described in demo.ui to the self object self.connect_signals (self) # create a group of hot keys, for keys with numbers and connect it to the window agroup = gtk.AccelGroup () self.window1.add_accel_group (agroup) # create buttons, hang hotkeys on them and place them in table1 for i in xrange (10): btn = gtk.Button (str (i)) btn.connect ('clicked', self.put_text) btn.add_accelerator ('clicked', agroup, ord (str (i)), 0, 0) y, x = i / 3, i% 3 self.table1.attach (btn, x, x + 1, y + 1, y + 2) # show the window and all the widgets on it self.window1.show_all () def __getattr __ (self, attr): # it is more convenient to write self.window1 than self.get_object ('window1') obj = self.get_object (attr) if not obj: raise AttributeError ('object% r has no attribute% r'% (self, attr)) setattr (self, attr, obj) return obj # Python descriptor that allows us to work with values ​​in gtk much more conveniently. Entry expr = EntryDescriptor ('entry1') def calculate (self, widget = None): if not self.expr: return try: self.expr = str (eval (self.expr)) except Exception, e: # we can close to zero, etc. print e def reset (self, widget): self.expr = '' def quit (self, widget): gtk.main_quit () def put_text (self, widget): text = widget.get_label () expr = self.expr if not expr [-1:]. isdigit () and not text.isdigit (): # so that you cannot write 8 / * 2, etc. return self.expr + = text def key_press (self, widget, event): # get the name of the button from its code key = gtk.gdk.keyval_name (event.keyval) if key == 'BackSpace': if self.expr: self.expr = self.expr [: - 1] elif key == 'Escape': self.reset (None) if __name__ == '__main__': calculator = Calculator () gtk.main ()
Source: https://habr.com/ru/post/87327/
All Articles