Gtk3 replace child widget with another widget

后端 未结 4 1679
执念已碎
执念已碎 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: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)
    

提交回复
热议问题