Skip to content

Instantly share code, notes, and snippets.

@carlos-jenkins
Created January 14, 2015 00:15
Show Gist options
  • Save carlos-jenkins/c4fedad66169b75424a0 to your computer and use it in GitHub Desktop.
Save carlos-jenkins/c4fedad66169b75424a0 to your computer and use it in GitHub Desktop.
from gi.repository import Gtk
from os.path import abspath, dirname, join
class MyApp(object):
def __init__(self):
"""
Build GUI
"""
# Build GUI from Glade file
self.builder = Gtk.Builder()
self.builder.add_from_file(
join(abspath(dirname(__file__)), 'ui.glade')
)
# Get objects
go = self.builder.get_object
self.window = go('window')
self.view = go('view')
self.delete = go('delete')
# 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 _delete_cb(self, widget, data=None):
"""
Button callback
"""
print('Delete button pressed...')
selection = self.view.get_selection()
model, paths = selection.get_selected_rows()
print([p.to_string() for p in paths])
for p in reversed(paths):
itr = model.get_iter(p)
model.remove(itr)
if __name__ == '__main__':
gui = MyApp()
Gtk.main()
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkListStore" id="model">
<columns>
<!-- column-name key -->
<column type="guint"/>
<!-- column-name value -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0">0</col>
<col id="1" translatable="yes">Zero</col>
</row>
<row>
<col id="0">1</col>
<col id="1" translatable="yes">One</col>
</row>
<row>
<col id="0">2</col>
<col id="1" translatable="yes">Two</col>
</row>
<row>
<col id="0">3</col>
<col id="1" translatable="yes">Three</col>
</row>
<row>
<col id="0">4</col>
<col id="1" translatable="yes">Four</col>
</row>
<row>
<col id="0">5</col>
<col id="1" translatable="yes">Five</col>
</row>
<row>
<col id="0">6</col>
<col id="1" translatable="yes">Six</col>
</row>
</data>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="border_width">10</property>
<property name="title" translatable="yes">Removing Rows in GtkTreeView</property>
<property name="window_position">center-always</property>
<property name="default_width">600</property>
<property name="default_height">400</property>
<child>
<object class="GtkBox" id="body">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkScrolledWindow" id="scroll">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">model</property>
<property name="enable_search">False</property>
<property name="tooltip_column">1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="selection">
<property name="mode">multiple</property>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="column_key">
<property name="resizable">True</property>
<property name="title" translatable="yes">Key</property>
<child>
<object class="GtkCellRendererText" id="cell_key"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="column_value">
<property name="resizable">True</property>
<property name="title" translatable="yes">Value</property>
<child>
<object class="GtkCellRendererText" id="cell_value"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButtonBox" id="buttons">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="homogeneous">True</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="delete">
<property name="label" translatable="yes">Delete</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="_delete_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment