Skip to content

Instantly share code, notes, and snippets.

@carlos-jenkins
Last active June 29, 2021 08:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlos-jenkins/5467657 to your computer and use it in GitHub Desktop.
Save carlos-jenkins/5467657 to your computer and use it in GitHub Desktop.
My PyGObject template files for simple applications.
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="border_width">10</property>
<property name="title" translatable="yes">Ejemplo PyGtk</property>
<property name="window_position">center-always</property>
<property name="default_width">400</property>
<property name="default_height">300</property>
<child>
<object class="GtkVBox" id="body">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">5</property>
<child>
<object class="GtkImage" id="image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="pixbuf">logo.png</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">&lt;span foreground="#2075AD" size="xx-large" weight="bold"&gt;This is a label&lt;/span&gt;</property>
<property name="use_markup">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkHButtonBox" id="footer">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">Please do click</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="_btn_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
# -*- coding:utf-8 -*-
#
# Copyright (C) 2013 Carlos Jenkins <carlos@jenkins.co.cr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
PyGObject example with Glade and GtkBuilder.
Check dependencies are installed:
sudo apt-get install python2.7 python-gi gir1.2-gtk-3.0
To execute:
python main.py
Python reference still unavailable, nevertheless C reference documentation is
available at:
https://developer.gnome.org/gtk3/
And a good tutorial at:
https://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html
"""
from gi.repository import Gtk
from os.path import abspath, dirname, join
WHERE_AM_I = abspath(dirname(__file__))
class MyApp(object):
def __init__(self):
"""
Build GUI
"""
# Declare states / variables
self.counter = 0
# Build GUI from Glade file
self.builder = Gtk.Builder()
self.glade_file = join(WHERE_AM_I, 'gui.glade')
self.builder.add_from_file(self.glade_file)
# Get objects
go = self.builder.get_object
self.window = go('window')
self.button = go('button')
# Connect signals
self.builder.connect_signals(self)
# Configure interface
self.window.connect('delete-event', lambda x,y: Gtk.main_quit())
# Everything is ready
self.window.show()
def _btn_cb(self, widget, data=None):
"""
Button callback
"""
self.counter += 1
print('Button pressed {} times.'.format(self.counter))
if __name__ == '__main__':
gui = MyApp()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment