Gtk3将子部件替换为另一个部件 [英] Gtk3 replace child widget with another widget

查看:133
本文介绍了Gtk3将子部件替换为另一个部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法从它的父母(无论可能是什么 - VBox,一个网格...)中移除一个小部件,并在其位置添加一个替代小部件。

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.

我找到了这个答案,但我似乎无法使它与Gtk3一起工作。

I found this answer, but I can't seem to make it work with Gtk3.

以下是我试过的内容:

Here's what I tried:

from gi.repository import Gtk

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_with_properties(new, **props)

但是调用 Gtk.ContainerClass.list_child_properties 引发

TypeError: argument self: Expected a Gtk.ContainerClass, but got gi.repository.Gtk.GObjectMeta

它不会接受一个容器小部件的实例。对于我的生活,我无法弄清楚我应该传递什么参数。

It won't accept an instance of a container widget either. For the life of me, I can't figure out what parameter I'm supposed to pass.

PS:我知道我可以在容器和孩子小部件,但我宁愿不要。

P.S.: I know I could add another widget between the container and the child widget, but I'd prefer not to.

更新:我认为它不够清楚:替代小部件应该与原始小部件位于同一位置,具有相同的打包属性。

UPDATE: I guess it wasn't clear enough: The substitute widget is supposed to be in the same place as the original widget, with the same packing properties.

推荐答案

这只是最近才成为可能,因为PyGObject 3.14: https://git.gnome。 org / browse / pygobject / commit /?h = pygobject-3-14& id = bf84915f89fd5fd502b4fb162eef7bc0a48c8783

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

以下任一项都可以在PyGObject 3.14及更高版本中使用:

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

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

(s第二种语法是在类和实例方法有命名冲突的情况下。 此处。)

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

这篇关于Gtk3将子部件替换为另一个部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆