I have just started Python, about 2 weeks ago. Now, I am trying to create GUIs with PyGObject using Glade.
However, I am puzzled on how the general layout of the program should be.
Should I use a class for the main program and the signals or should I separate them?
Is there a "best approach" for this?
Or as in below humble approach of mine, should I not use classes at all?
How do I communicate between functions in the below example? For example, how do I set parent
parameter of Gtk.MessageDialog
function as the main window of the program?
Python Code:
#!/usr/bin/python
try:
from gi.repository import Gtk
except:
print('Cannot Import Gtk')
sys.exit(1)
# Confirm and exit when Quit button is clicked.
def on_button_quit_clicked(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(widget):
print ('About')
# Perform addition when button is clicked.
def on_button_add_clicked(widget):
print ('Add')
# Main function
def main():
builder = Gtk.Builder()
builder.add_from_file('CalculatorGUI.glade')
signalHandler = {
'on_main_window_destroy': Gtk.main_quit,
'on_button_quit_clicked': on_button_quit_clicked,
'on_button_about_clicked': on_button_about_clicked,
'on_button_add_clicked': on_button_add_clicked
}
builder.connect_signals(signalHandler)
main_window = builder.get_object('main_window')
main_window.show_all()
Gtk.main()
print ('Program Finished!')
# If the program is not imported as a module, then run.
if __name__ == '__main__':
main()
Ingredients of CalculatorGUI.glade
file: http://pastebin.com/K2wb7Z4r
A screenshot of the program: