Reputation: 1494
I'm trying to make a gtk.TreeView with four columns, where the first two columns can be resized and the last two can't. In other words, I want the third and fourth columns to occupy a fixed amount of space and I want the first and second columns to share the remaining space in whatever proportion the user wants.
My problem is that I've been unable to prevent the last column from growing and shrinking. The code below should highlight this problem: if you run this code and try to resize either the first or second columns, the last column will make up the difference.
I know what I want can be done, because I've seen other GTK GUIs that do it. Two examples are the "signals" dialog in glade and the "song list" in quodlibet. Thanks for any ideas on how to get this working.
import pygtk; pygtk.require('2.0')
import gtk
import pango
window = gtk.Window()
model = gtk.TreeStore(object)
view = gtk.TreeView(model)
columns = [
('first', True),
('second', True),
('third', False),
('fourth', False),
]
rows = [
{
'first': 'Lorem ipsum dolor sit amet',
'second': 'consectetur adipiscing elit',
'third': 'Nam justo sem',
'fourth': 'malesuada ut ultricies ac',
}, {
'first': 'Praesent sit amet nibh turpis',
'second': 'vitae lacinia metus',
'third': 'Ut nisi lacus',
'fourth': 'pretium a diam',
}
]
for row in rows:
model.append(None, (row,))
def cell_data_func(column, renderer, model, iter, field):
text = model.get_value(iter, 0)[field]
renderer.set_property('text', text)
for field, expand in columns:
text_renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn(field, text_renderer)
column.set_cell_data_func(text_renderer, cell_data_func, field)
if expand:
column.set_expand(True)
column.set_resizable(True)
text_renderer.set_property('ellipsize', pango.ELLIPSIZE_END)
else:
column.set_expand(False)
column.set_resizable(False)
view.append_column(column)
window.set_default_size(700, -1)
window.add(view)
window.show_all()
window.connect('destroy', lambda *args: gtk.main_quit())
gtk.main()
Upvotes: 2
Views: 704
Reputation: 18109
Here is your code modified with a Horrizontal Box wrapper:
import pygtk; pygtk.require('2.0')
import gtk
import pango
window = gtk.Window()
model = gtk.TreeStore(object)
view = gtk.TreeView(model)
columns = [
('first', True),
('second', True),
('third', False),
('fourth', False),
]
rows = [
{
'first': 'Lorem ipsum dolor sit amet',
'second': 'consectetur adipiscing elit',
'third': 'Nam justo sem',
'fourth': 'malesuada ut ultricies ac',
}, {
'first': 'Praesent sit amet nibh turpis',
'second': 'vitae lacinia metus',
'third': 'Ut nisi lacus',
'fourth': 'pretium a diam',
}
]
for row in rows:
model.append(None, (row,))
def cell_data_func(column, renderer, model, iter, field):
text = model.get_value(iter, 0)[field]
renderer.set_property('text', text)
for field, expand in columns:
text_renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn(field, text_renderer)
column.set_cell_data_func(text_renderer, cell_data_func, field)
if expand:
column.set_expand(True)
column.set_resizable(True)
text_renderer.set_property('ellipsize', pango.ELLIPSIZE_END)
else:
column.set_expand(False)
column.set_resizable(False)
view.append_column(column)
window.set_default_size(700, -1)
vertial_panes = gtk.HBox()
vertial_panes.pack_start(view, expand=False)
right_hand_space = gtk.HBox()
vertial_panes.pack_start(right_hand_space, expand=False)
window.add(vertial_panes)
window.show_all()
window.connect('destroy', lambda *args: gtk.main_quit())
gtk.main()
The concept being that the tableview will allocate only as much width as its needs because of its expand=False
. The right_hand_space can be any element, and its expand status does not matter as GTK will make the parent HBox fill the available width.
Upvotes: 1
Reputation: 6637
Add fifth column without any text and by can expanding feature and enjoy :)
.
.
.
columns = [
('first', True),
('second', True),
('third', False),
('fourth', False),
('', True),
]
.
.
.
Upvotes: 2