Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- try:
- from gi.repository import Gtk
- except ImportError:
- print('Cannot Import Gtk')
- sys.exit(1)
- class GUI():
- # Initialize GUI
- def __init__(self):
- # Build from .glade file
- self.builder = Gtk.Builder()
- self.builder.add_from_file('CalculatorGUI.glade')
- # Create and connect signals
- signalHandler = {
- 'on_main_window_destroy': Gtk.main_quit,
- 'on_button_quit_clicked': self.on_button_quit_clicked,
- 'on_button_about_clicked': self.on_button_about_clicked,
- 'on_button_add_clicked': self.on_button_add_clicked,
- 'about_response':self.about_response
- }
- self.builder.connect_signals(signalHandler)
- # Create main window object.
- self.main_window = self.builder.get_object('main_window')
- # Create tags to entries for easy acces
- self.first_number = self.builder.get_object('entry_first_number')
- self.second_number = self.builder.get_object('entry_second_number')
- self.result_label = self.builder.get_object('label_result')
- self.image_warning = self.builder.get_object('image_warning')
- self.label_warning = self.builder.get_object('label_warning')
- self.about_dialog = self.builder.get_object('aboutdialog')
- # Confirm and exit when Quit button is clicked.
- def on_button_quit_clicked(self, widget):
- confirmation_dialog = Gtk.MessageDialog(parent = None,
- flags = Gtk.DialogFlags.DESTROY_WITH_PARENT,
- type = Gtk.MessageType.QUESTION,
- buttons = Gtk.ButtonsType.YES_NO,
- message_format =
- 'Are you sure you want to quit?')
- print('Quit confirmation dialog is running.')
- confirmation_response = confirmation_dialog.run()
- if confirmation_response == Gtk.ResponseType.YES:
- print('You have clicked on YES, quiting..')
- Gtk.main_quit()
- elif confirmation_response == Gtk.ResponseType.NO:
- print('You have clicked on NO')
- confirmation_dialog.destroy()
- print('Quit confirmation dialog is destroyed.')
- # Show About dialog when button is clicked.
- def on_button_about_clicked(self, widget):
- self.about_dialog.present()
- print('About dialog opened.')
- # About dialog "response" signal.
- def about_response(self, widget, response_id, user_data=None):
- # self.about_dialog.destroy()
- print(widget, response_id, user_data)
- print('About dialog closed.')
- # Perform addition when button is clicked.
- def on_button_add_clicked(self, widget):
- print('Adding two numbers..')
- try:
- first_number = float(self.first_number.get_text())
- second_number = float(self.second_number.get_text())
- result = first_number + second_number
- print('First number is: {0}. Second Number is: {1}.\nResult is: {2}.'
- . format(first_number, second_number, result))
- self.result_label.set_text('{:.10g}'.format(result))
- self.image_warning.hide()
- self.label_warning.hide()
- except ValueError:
- print('Could not convert strings to integers in entries.')
- self.label_warning.set_text('Could not convert strings to integers in entries.')
- self.image_warning.show()
- self.label_warning.show()
- except:
- print('Could not do the addition; ' + str(sys.exc_info()[1]))
- self.label_warning.set_text('Could not do the addition.')
- self.image_warning.show()
- self.label_warning.show()
- # Main function
- def main():
- myGUI = GUI()
- myGUI.main_window.show_all()
- Gtk.main()
- print ('Program Finished!')
- # If the program is not imported as a module, then run.
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement