Created
May 10, 2013 15:45
-
-
Save carlos-jenkins/5555283 to your computer and use it in GitHub Desktop.
Conditional CellRenderer visibility to display different models in a TreeView.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<interface> | |
<!-- interface-requires gtk+ 3.0 --> | |
<object class="GtkTreeStore" id="treestore"> | |
<columns> | |
<!-- column-name name --> | |
<column type="gchararray"/> | |
<!-- column-name catalog_num --> | |
<column type="gchararray"/> | |
<!-- column-name comment --> | |
<column type="gchararray"/> | |
<!-- column-name component --> | |
<column type="gchararray"/> | |
<!-- column-name is_top --> | |
<column type="gboolean"/> | |
<!-- column-name is_child --> | |
<column type="gboolean"/> | |
</columns> | |
</object> | |
<object class="GtkWindow" id="window"> | |
<property name="can_focus">False</property> | |
<property name="border_width">10</property> | |
<property name="title" translatable="yes">TreeView test</property> | |
<property name="window_position">center-always</property> | |
<property name="default_width">400</property> | |
<property name="default_height">300</property> | |
<child> | |
<object class="GtkScrolledWindow" id="scrolledwindow"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="shadow_type">in</property> | |
<child> | |
<object class="GtkTreeView" id="treeview"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="model">treestore</property> | |
<property name="headers_visible">False</property> | |
<property name="headers_clickable">False</property> | |
<property name="search_column">1</property> | |
<property name="tooltip_column">2</property> | |
<child internal-child="selection"> | |
<object class="GtkTreeSelection" id="treeview-selection"/> | |
</child> | |
<child> | |
<object class="GtkTreeViewColumn" id="treeviewcolumn_name"> | |
<property name="title" translatable="yes">Name</property> | |
<child> | |
<object class="GtkCellRendererText" id="cellrenderertext_name"/> | |
<attributes> | |
<attribute name="visible">4</attribute> | |
<attribute name="text">0</attribute> | |
</attributes> | |
</child> | |
<child> | |
<object class="GtkCellRendererText" id="cellrenderertext_component"/> | |
<attributes> | |
<attribute name="visible">5</attribute> | |
<attribute name="text">3</attribute> | |
</attributes> | |
</child> | |
</object> | |
</child> | |
<child> | |
<object class="GtkTreeViewColumn" id="treeviewcolumn_catalog_num"> | |
<property name="title" translatable="yes">Catalog #</property> | |
<child> | |
<object class="GtkCellRendererText" id="cellrenderertext_catalog"/> | |
<attributes> | |
<attribute name="visible">4</attribute> | |
<attribute name="text">1</attribute> | |
</attributes> | |
</child> | |
</object> | |
</child> | |
</object> | |
</child> | |
</object> | |
</child> | |
</object> | |
</interface> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
# | |
# Copyright (C) 2013 Carlos Jenkins <[email protected]> | |
# | |
# 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/>. | |
""" | |
Conditional display of TreeStore columns in TreeView. | |
""" | |
from gi.repository import Gtk | |
from os.path import abspath, dirname, join | |
WHERE_AM_I = abspath(dirname(__file__)) | |
# My Model map | |
mm = { | |
'name' : 0, | |
'catalog_num' : 1, | |
'comment' : 2, | |
'component' : 3, | |
'is_top' : 4, | |
'is_child' : 5, | |
} | |
class MyApp(object): | |
def __init__(self): | |
""" | |
Build GUI | |
""" | |
# 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.treestore = go('treestore') | |
# Fill model | |
self._load_model() | |
# Connect signals | |
self.builder.connect_signals(self) | |
self.window.connect('delete-event', lambda x,y: Gtk.main_quit()) | |
# Everything is ready | |
self.window.show() | |
def _load_model(self): | |
my_data = [ | |
['Cheese', 'F001', 'This is the best cheese ever!', '', True, False], | |
['Pepperoni', 'F002', 'Delicious pepperoni :}', '', True, False], | |
['Pepperonni Pizza', 'P001', 'Yes, I\'m hungry :(', '', True, False], | |
['', '', '', 'Cheese', False, True], | |
['', '', '', 'Pepperonni', False, True], | |
] | |
parent = None | |
for i in my_data: | |
if i[mm['is_child']]: | |
self.treestore.append(parent, i) | |
else: | |
parent = self.treestore.append(None, i) | |
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