On 8/14/07, Darren Hart <[EMAIL PROTECTED]> wrote:
>
> I'm trying to create a widget that lists checkbuttons with labels in a
> tabular format that will adjust it's layout as the window is resized. I am
> currently using a table widget and have a working resize() method.
> Unfortunately, I haven't been able to capture any kind of a resize event
> other than size_allocate() which unfortunately caused an endless loop since
> my resize()ing the table caused another size_allocate() call, etc etc.
>
> Reading the docs suggest that what I want to use is a configure_event
> handler and that table is one of those special widgets with no XWindow of
> it's own, that therefor will not receive configure_events. I added my table
> to an eventbox as mentioned in one of the tutorials, but I am not receiving
> any configure events there either. I tried adding the button_press_event
> for a sanity check, and it is working fine. I expect that the configure
> event should occur when I resize my main window and the table expands as
> it's container does (the column spacing increases).
>
> I am using libglade to build the interface, and am experimenting with
> aggregation (vs. inheritance) for my object model. To connect the signals,
> the WidgetWrapper base class calls: signal_autoconnect(self) on the newly
> constructed aggregate widget (and this is working well for the other
> widgets, as well as for the button_press_event handler in this widget).
> I've removed some superfluous code from the following example for clarity
> (and it certainly isn't usable without all the requisite other classes). I
> did confirm that "on_configure_event" is the name I gave the handler in
> glade, and I even tried connecting it manually via widget.connect() with
> the same results.
>
> Can someone offer some insight as to what I might be doing wrong? Is my
> entire approach just not appropriate for pygtk?
>
>
Attached is a stripped down example (without libglade or my app logic) of a
self resizing table widget that needs to know when
it has been resized. The code is commented to indicate the problem - It
works when I bind the window configure_event tot he table update_size
method, but not when I bind the eventbox configure_event. Obviously the
widget isn't usually placed in a top level window, so I need to know when
just it's immediate parent container get's resized.
Any feedback would be most appreciated. Thanks.
--
Darren Hart
#!/usr/bin/env python
# example autotable.py (based partly on eventbox.py)
import pygtk
pygtk.require('2.0')
import gtk
class AutoTable(gtk.Table):
def __init__(self, cells):
gtk.Table.__init__(self)
self.labels = []
for c in cells:
self.labels.append(gtk.Label(c))
self.update_size()
def update_size(self):
# determine the widest cell needed
max_width = 0
for l in self.labels:
w = l.size_request()[0]
if w > max_width:
max_width = w
if l.parent:
self.remove(l)
# resize it (with forced col spacing of 5)
pitch = self.allocation.width / (max_width+5)
if pitch == 0:
pitch = 1
self.resize(pitch, len(self.labels)/pitch + 1)
i=0
for l in self.labels:
x = i % pitch
y = i / pitch
self.attach(l, x, x+1, y, y+1)
l.show()
i = i + 1
print "new size: ", self.allocation.width
print "new pitch: ", pitch
print "updated layout for: ", pitch, "x", self.get_property("n-columns")
# eventbox callbacks
def on_configure_event(self, widget, event):
print "configure_event received"
self.update_size()
return False
class EventBoxExample:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("AutoTable Example")
window.connect("destroy", lambda w: gtk.main_quit())
window.set_border_width(10)
# Create an EventBox and add it to our toplevel window
event_box = gtk.EventBox()
event_box.set_events(gtk.gdk.STRUCTURE_MASK)
event_box.set_visible_window(True)
window.add(event_box)
event_box.show()
# Create the AutoTable
auto_table = AutoTable(["one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten"])
event_box.add(auto_table)
auto_table.show()
# Bind callbacks
# FIXME: I need to be able to reconfigure the table when the table
# itself is resized (or reconfigured). I thought the event box
# was supposed to make this possible, but it doesn't seem to be
# receiving the configure events either. The AutoTable may be
# placed in something like an hpaned widget, witch can change
# the table's size without changing the window size.
#event_box.connect("configure_event", auto_table.on_configure_event)
window.connect("configure_event", auto_table.on_configure_event)
window.connect("destroy", lambda w: gtk.main_quit)
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
EventBoxExample()
main()
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/