Files
YMS/yms/treeview.py

44 lines
1.3 KiB
Python

from gi.repository import Gtk
def add_column(treeview, label, index, xalign=None, col_fg=None, col_bg=None, visible=True):
col = Gtk.TreeViewColumn(label)
cell = Gtk.CellRendererText()
col.pack_start(cell, True)
col.add_attribute(cell, 'text', index)
if xalign:
cell.set_property('xalign', xalign)
if col_fg:
col.add_attribute(cell, 'foreground', col_fg)
if col_bg:
col.add_attribute(cell, 'background', col_bg)
treeview.append_column(col)
def get_value(treeview, column=0):
"""
Returns the value of the specified column of the linked model
"""
try:
selection = treeview.get_selection()
except:
return None
if selection.get_mode() == Gtk.SelectionMode.SINGLE:
(model, iter) = selection.get_selected()
if iter:
return model.get_value(iter, column)
return None
def set_value(treeview, column, value):
"""
Sets the value of the specified column of the linked model
"""
try:
selection = treeview.get_selection()
except:
return False
if selection.get_mode() == Gtk.SelectionMode.SINGLE:
(model, iter) = selection.get_selected()
model.set_value(iter, column, value)
return True
else:
return False