我正在寻找一种方法从其父级(无论可能是什么 - 一个VBox,一个网格......)中删除一个小部件,并在其位置添加一个替代小部件.
我找到了这个答案,但我似乎无法使其与Gtk3一起使用.
这是我试过的:
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)
Run Code Online (Sandbox Code Playgroud)
但是要求Gtk.ContainerClass.list_child_properties加油
TypeError: argument self: Expected a Gtk.ContainerClass, but got gi.repository.Gtk.GObjectMeta
Run Code Online (Sandbox Code Playgroud)
它也不会接受容器小部件的实例.对于我的生活,我无法弄清楚我应该通过什么参数.
PS:我知道我可以在容器和子窗口小部件之间添加另一个窗口小部件,但我不想这样做.
更新:我想这还不够清楚:替代小部件应该与原始小部件位于同一个地方,具有相同的打包属性.
这是一个已知的错误/问题,当父级更改时,GTK中的标签不会动态调整大小.这是一个非常烦人的小细节之一,如果可能的话,我想破解它.
我按照16软件的方法进行了操作,但根据免责声明,您无法将其调整得更小.所以我尝试了其中一个注释(set_size_request信号回调中的调用)中提到的技巧,但这导致了某种无限循环(尝试并看到).
有没有人有任何其他想法?
(您不能仅在调用期间阻止信号,因为正如print语句似乎表明的那样,问题在函数离开后开始.)
代码如下.你可以看到我的意思,如果你运行它并尝试调整窗口大小然后更小.(如果要查看原始问题,请在"连接到大小分配信号"之后注释掉该行,运行它,然后将窗口调整得更大.)
Glade文件("example.glade"):
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<property name="visible">True</property>
<signal name="destroy" handler="on_destroy"/>
<child>
<widget class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">In publishing and graphic design, lorem ipsum[p][1][2] is the name given to commonly used placeholder text (filler text) to demonstrate the graphic elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum …Run Code Online (Sandbox Code Playgroud)