Advertisement
Guest User

Untitled

a guest
Apr 26th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4.  
  5. try:
  6.     from gi.repository import Gtk
  7. except ImportError:
  8.     print('Cannot Import Gtk')
  9.     sys.exit(1)
  10.  
  11.  
  12. class GUI():
  13.     # Initialize GUI
  14.     def __init__(self):
  15.         # Build from .glade file
  16.         self.builder = Gtk.Builder()
  17.         self.builder.add_from_file('CalculatorGUI.glade')
  18.        
  19.         # Create and connect signals
  20.         signalHandler = {
  21.         'on_main_window_destroy': Gtk.main_quit,
  22.         'on_button_quit_clicked': self.on_button_quit_clicked,
  23.         'on_button_about_clicked': self.on_button_about_clicked,
  24.         'on_button_add_clicked': self.on_button_add_clicked,
  25.         'about_response':self.about_response
  26.         }
  27.         self.builder.connect_signals(signalHandler)
  28.        
  29.         # Create main window object.
  30.         self.main_window = self.builder.get_object('main_window')
  31.        
  32.         # Create tags to entries for easy acces
  33.         self.first_number = self.builder.get_object('entry_first_number')
  34.         self.second_number = self.builder.get_object('entry_second_number')
  35.         self.result_label = self.builder.get_object('label_result')
  36.         self.image_warning = self.builder.get_object('image_warning')
  37.         self.label_warning = self.builder.get_object('label_warning')
  38.         self.about_dialog = self.builder.get_object('aboutdialog')
  39.        
  40.     # Confirm and exit when Quit button is clicked.
  41.     def on_button_quit_clicked(self, widget):
  42.         confirmation_dialog = Gtk.MessageDialog(parent = None,
  43.                                                 flags = Gtk.DialogFlags.DESTROY_WITH_PARENT,
  44.                                                 type = Gtk.MessageType.QUESTION,
  45.                                                 buttons = Gtk.ButtonsType.YES_NO,
  46.                                                 message_format =
  47.                                                 'Are you sure you want to quit?')
  48.         print('Quit confirmation dialog is running.')
  49.         confirmation_response = confirmation_dialog.run()                                              
  50.         if confirmation_response == Gtk.ResponseType.YES:
  51.             print('You have clicked on YES, quiting..')
  52.             Gtk.main_quit()
  53.         elif confirmation_response == Gtk.ResponseType.NO:
  54.             print('You have clicked on NO')
  55.         confirmation_dialog.destroy()
  56.         print('Quit confirmation dialog is destroyed.')
  57.    
  58.     # Show About dialog when button is clicked.
  59.     def on_button_about_clicked(self, widget):
  60.         self.about_dialog.present()
  61.         print('About dialog opened.')
  62.    
  63.     # About dialog "response" signal.
  64.     def about_response(self, widget, response_id, user_data=None):
  65.         # self.about_dialog.destroy()
  66.         print(widget, response_id, user_data)
  67.         print('About dialog closed.')
  68.    
  69.     # Perform addition when button is clicked.
  70.     def on_button_add_clicked(self, widget):
  71.         print('Adding two numbers..')
  72.         try:
  73.             first_number = float(self.first_number.get_text())
  74.             second_number = float(self.second_number.get_text())
  75.             result = first_number + second_number
  76.             print('First number is: {0}. Second Number is: {1}.\nResult is: {2}.'
  77.                     . format(first_number, second_number, result))
  78.             self.result_label.set_text('{:.10g}'.format(result))
  79.             self.image_warning.hide()
  80.             self.label_warning.hide()
  81.         except ValueError:
  82.             print('Could not convert strings to integers in entries.')
  83.             self.label_warning.set_text('Could not convert strings to integers in entries.')
  84.             self.image_warning.show()
  85.             self.label_warning.show()
  86.         except:
  87.             print('Could not do the addition; ' + str(sys.exc_info()[1]))
  88.             self.label_warning.set_text('Could not do the addition.')
  89.             self.image_warning.show()
  90.             self.label_warning.show()
  91.            
  92.        
  93.    
  94. # Main function
  95. def main():
  96.     myGUI = GUI()
  97.     myGUI.main_window.show_all()
  98.    
  99.     Gtk.main()
  100.    
  101.     print ('Program Finished!')
  102.  
  103. # If the program is not imported as a module, then run.
  104. if __name__ == '__main__':
  105.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement