tags:

views:

54

answers:

2

I'm working on a Python application involving the use of a GTK table. The application requires that widgets of various sizes be added to a table dynamically. Because of this, I need to be able to ask the table what cells are in use (more accurately, NOT in use) so that I know where I can place a new widget without overlapping.

Based on the information in the reference manual (http://www.pygtk.org/docs/pygtk/) I have been unable to find a way to get that information directly from the table. The only other option I can think of is to create a map object that holds used cell information, and have it updated upon changes to the table.

Since I'm sure someone has dealt with this before me, and I would hope GTK would provide a better way, it seemed wise to ask around before trying to implement the map.

Help would be greatly appreciated.

+1  A: 

This function should give you a set of the free cells in the table:

def free_cells(table):
    free_cells = set([(x,y) for x in range(table.props.n_columns) for y in range(table.props.n_rows)])

    def func(child):
        (l,r,t,b) = table.child_get(child, 'left-attach','right-attach','top-attach','bottom-attach')
        used_cells = set([(x,y) for x in range(l,r) for y in range(t,b)])
        free_cells.difference_update(used_cells)

    table.foreach(func)

    return free_cells

It starts with a set of all the table cells, then iterates over the children of the table, removing the cells occupied by each child.

Geoff Reedy
A: 

I'm the original poster, was logged into wrong account when posting question.

Anyway, this appears to be exactly what I'm looking for! Thanks Geoff!

Josh
Don't forget to click the green checkmark if it's the right answer to your question.
ptomato