'How to expand row with text on click in Gtk.Treeview cell ? it's possible?
I have problem with trevieew, i need example to expand row by cell click on, similary to Gtk.Expander with arrow. Its that possible ? I put long string to column "Data", and i want show all / hide this text on click. I tried used Gtk.Expander, but this widget is incompatibility with gtk.TreeView.
Solution 1:[1]
The trick to getting rows in a Gtk.TreeView to expand with an arrow similar to Gtk.Expander is to add some rows as 'top level' and some rows as 'children'. This relationship is managed using the Gtk.TreeIter objects that are returned by Gtk.TreeStore.append(). When this relationship is established, TreeView automatically adds arrows beside collapsible rows. For example:
def add_data(self, data):
""" Add data to the Gtk.TreeStore """
for key, value in data.items():
top_level_row = self.tree_store.append(None, [key, ""])
for item in value:
child_row = self.tree_store.append(top_level_row,[item[0], item[1]])
In this function, data is sent as a dictionary. The key is set as a top-level row by passing 'None' as the first value to the .append() function. This has the effect of setting the root node of the tree store as the parent for this row. The .append() function returns a Gtk.TreeIter object that points to this new row.
The key's value (a list of student values) is iterated over and each list item is set as a child of this row. This is accomplished by passing the Gtk.TreeIter of the parent row ('top_level_row') as the first parameter of the .append() function for each child of that row.
A ready-to-run example program:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
student_data = {'Grade 8': [
['Simpson, Lisa', 'F'],
['Bueller, Feris', 'M'],
['Standish, Claire', 'F']
],
'Grade 9': [
['Wiggum, Ralph', 'M'],
['Frye, Cameron', 'M'],
['Vernon, Richard', 'M']
]}
class TreeViewExample(Gtk.Window):
TREE_ROOT = None # Index value for the tree's root node
NAME = 0 # Column index for 'name' data
GENDER = 1 # Column index for 'gender' data
def __init__(self):
# Initialize window
Gtk.Window.__init__(self, title="TreeView")
self.set_size_request(300, 300)
self.set_position(Gtk.WindowPosition.CENTER)
self.tree_store = Gtk.TreeStore(str, str) # Create a Gtk.TreeStore to hold data
self.add_data(student_data) # Add data to the TreeStore
self.treeview = Gtk.TreeView(model=self.tree_store) # Create the Gtk.TreeView container
self.add(self.treeview) # Add the TreeView to the window
# CellRenderer for text
text_renderer = Gtk.CellRendererText()
# Create a TreeView column to hold student names
col_combined = Gtk.TreeViewColumn("Student")
col_combined.pack_start(text_renderer, False)
col_combined.add_attribute(text_renderer, "text", self.NAME)
self.treeview.append_column(col_combined)
# Create a TreeView column to hold student genders
col_combined = Gtk.TreeViewColumn("Gender")
col_combined.pack_start(text_renderer, False)
col_combined.add_attribute(text_renderer, "text", self.GENDER)
self.treeview.append_column(col_combined)
def add_data(self, data):
""" Add data to the Gtk.TreeStore """
for key, value in data.items():
top_level_row = self.tree_store.append(None, [key, ""])
for item in value:
child_row = self.tree_store.append(top_level_row,
[item[self.NAME],
item[self.GENDER]])
win = TreeViewExample()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |