Home Code Python Class Template example for GTK+ GUI Application built using Glade

Python Class Template example for GTK+ GUI Application built using Glade

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()
Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,248FansLike
10FollowersFollow
153FollowersFollow
377FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

An Efficient Strategy to fight Corona Virus

By setting up local police station level online delivery networks for essential items like grocery, medicines - the need for general public to come out of their homes can be removed.

Rising pollution can cause Pandemics deadlier than Corona Virus

Rising pollution can expose ancient killer microbes that may cause pandemics more deadlier than Corona. Melting Ice Sheets can resurrect killer agents of the past.

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?
Content Protection by DMCA.com