Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found the solution in the code of the <em>gazpacho</em> interface designer.</p> <p>You can use this function:</p> <pre><code>def replace_widget(current, new): """ Replace one widget with another. 'current' has to be inside a container (e.g. gtk.VBox). """ container = current.parent assert container # is "current" inside a container widget? # stolen from gazpacho code (widgets/base/base.py): props = {} for pspec in gtk.container_class_list_child_properties(container): props[pspec.name] = container.child_get_property(current, pspec.name) gtk.Container.remove(container, current) container.add(new) for name, value in props.items(): container.child_set_property(new, name, value) </code></pre> <p>The key seems to be to transfer the child properties from the old widget to the new one after running <code>gtk.Container.add()</code>.</p> <p>Applied to your example, this would be:</p> <pre><code>import pygtk import gtk def replace_widget(current, new): """ Replace one widget with another. 'current' has to be inside a container (e.g. gtk.VBox). """ container = current.parent assert container # is "current" inside a container widget? # stolen from gazpacho code (widgets/base/base.py): props = {} for pspec in gtk.container_class_list_child_properties(container): props[pspec.name] = container.child_get_property(current, pspec.name) gtk.Container.remove(container, current) container.add(new) for name, value in props.items(): container.child_set_property(new, name, value) def destroy(widget, data=None): gtk.main_quit() def wrap_in_button(label): text = label.get_text() button = gtk.Button(text) replace_widget(label, button) def Main(): # Pretend that this chunk is actually replaced by GTKBuilder work # From here... window = gtk.Window() window.connect('destroy', destroy) box = gtk.VBox() window.add(box) label1 = gtk.Label("Label 1") label2 = gtk.Label("Label 2") label3 = gtk.Label("Label 3") box.pack_start(label1) box.pack_start(label2) box.pack_start(label3) # ...up to here wrap_in_button(label2) window.show_all() gtk.main() if __name__ == "__main__": Main() </code></pre> <p>This works for me using Python 2.6.6 and PyGTK 2.17.</p> <p>As a solution to your original problem, I used <code>label_set_autowrap()</code> <a href="https://stackoverflow.com/questions/1893748/pygtk-dynamic-label-wrapping/2016849#2016849">from here</a> and it worked most of the time. However, it's not a perfect solution as I wasn't able to correctly right-align auto-wrapped text.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload