gpt4 book ai didi

python - PyGTK 小部件的就地替换

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:46 25 4
gpt4 key购买 nike

下面的代码创建了一列三个标签。我想使用中间的标签,在初始创建 UI 之后使用标签中的文本将其替换为另一个小部件。

我的实际用例是采用 GTKBuilder 填充的 UI,并将任何特定的命名标签替换为 dynamically wrapped label在运行时。 (我在这里使用了一个按钮,因为它简单但与众不同。)然后我仍然可以使用 Glade 来设置 UI,包括标签,如果我以后想要制作标签包装,就不会在我的 Python 代码中添加错误的标签和字符串。

目前的代码不起作用 - 新按钮被添加到列的末尾,我希望它保留在中间,label2 开始的地方。我该怎么做,最好是在 wrap_in_button 中,以确保它最终出现在正确的位置?我宁愿保持它的通用性,因为父级可能是 BoxTable 或任何通用的 Container

import pygtk
import gtk

def destroy(widget, data=None):
gtk.main_quit()

def wrap_in_button(label):
text = label.get_text()
button = gtk.Button(text)

parent = label.get_parent()

if parent:
parent.remove(label)
parent.add(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

# Comment this to see the original layout
wrap_in_button(label2)

window.show_all()

gtk.main()

if __name__ == "__main__":
Main()

最佳答案

我在gazpacho界面设计器的代码中找到了解决方案。

你可以使用这个函数:

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)

关键似乎是在运行 gtk.Container.add() 之后将子属性从旧小部件转移到新小部件。

应用于您的示例,这将是:

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()

这适用于使用 Python 2.6.6 和 PyGTK 2.17 的我。

作为您原来问题的解决方案,我使用了 label_set_autowrap() from here它大部分时间都有效。但是,这不是一个完美的解决方案,因为我无法正确右对齐自动换行的文本。

关于python - PyGTK 小部件的就地替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3107290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com