Gtk3 replace child widget with another widget

后端 未结 4 1640
执念已碎
执念已碎 2021-01-17 10:25

I\'m looking for a way to remove a widget from its parent (whatever that may be - a VBox, a Grid, ...) and add a substitute widget in its place.

I found this answer,

相关标签:
4条回答
  • 2021-01-17 10:34

    i know how it works with C , i did not try it with python :

     parent = gtk_widget_get_parent(old);
     g_object_ref(_key); /** because gtk_container_remove remove the widget with its reference we have to increment the number of reference if we want to reuse the old widget **/
     gtk_container_remove(GTK_CONTAINER(parent), old);
     gtk_container_add(GTK_CONTAINER(parent), new);
    

    PS : in the case of a VBox or Grid the widget is not inserted int the place of old one , you have to specify the position of insertion (the position of the old whild)

    good luck ,

    0 讨论(0)
  • 2021-01-17 10:38

    You can try reparent() method of Gtk.Widget. You can check docs at this link. If I remember well you should reparent child widget first, and then remove old parent widget. If it doesn't work post in comment and I'll check my docs (currently not on that machine sry).

    0 讨论(0)
  • 2021-01-17 10:41

    This has only recently become possible, since PyGObject 3.14: https://git.gnome.org/browse/pygobject/commit/?h=pygobject-3-14&id=bf84915f89fd5fd502b4fb162eef7bc0a48c8783

    Either of the following will work in PyGObject 3.14 and later:

    parent.list_child_properties()
    Gtk.ContainerClass.list_child_properties(parent)
    

    (The second syntax is in case class and instance methods have a naming conflict. See discussion here.)

    0 讨论(0)
  • 2021-01-17 10:51

    Thanks to the information provided by ptomato's answer, here's the working code:

    def replace_widget(old, new):
        parent = old.get_parent()
    
        props = {}
        for key in Gtk.ContainerClass.list_child_properties(type(parent)):
            props[key.name] = parent.child_get_property(old, key.name)
    
        parent.remove(old)
        parent.add(new)
    
        for name, value in props.iteritems():
            parent.child_set_property(new, name, value)
    
    0 讨论(0)
提交回复
热议问题