This article contains a bare bone Python Class Template, an example class or a sample class to load GUI built using Glade. The class can launch the GUI application. You can add Python event code functions to handle GTK+ GUI signals of UI objects like GTKApplicationWindow, GTJKListbox, GTKButton, GTKLabel, etc.
This class assumes that you have built your GUI interface using Glade, and the Glade file name is “MyGUI.glade”. This class also assumes that you have linked UI events or signals to Python function names using the signals tab of each GUI object in Glade. In the signals tab of Glade you can enter names of Python functions for all GUI events that object can handle like key events, mouse events and other user action events.
Use the below class as a base template for your GUI application and build your complete GUI application quickly. All thanks to Glade and Python – GUI programming has never been so easy on Linux or Ubuntu systems.
import gi #make sure you have latest GTK version gi.require_version('Gtk', '3.0') from gi.repository import Gtk class MyGUIApp: def __init__(self,glade_file): #Initialize the GTK Builder self.builder = Gtk.Builder() #Load GUI from the Glade File self.builder.add_from_file(glade_file) #Link all GUI events specified in Glade to corresponding functions in this class self.builder.connect_signals(self) def launch(self): #assuming your main window in Glade has ID winMain window = self.builder.get_object("winMain") window.show_all() window.maximize() Gtk.main() #make sure you have set destroy signal to "onDestroy" in Glade for "winMain" #else this function will not be called on close event of main application window def onDestroy(self, *args): #quit the application on closing of main application window Gtk.main_quit() #Write additional functions below to handle other events or signals #The function names can be linked to specific GUI events in Glade itself in the signals tab of each GUI object in Glade
You can initialize and launch the application as follows:
myapp = MyGuiApp("MyGUI.glade") myapp.launch()