PyGTK 小部件的就地替换 [英] In-place substitution of PyGTK widgets

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

问题描述

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

The code below creates a column of three labels. I would like to take the middle label, and replace it with another widget using the text from the label after the initial creation of the UI.

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

My actual use case is to take a GTKBuilder populated UI, and replace any particular named label with a dynamically wrapped label at run time. (I used a button here because it's simple but distinct.) Then I can still use Glade to set up the UI, including the labels, and not pepper my Python code with errant labels and strings if I later want to make a label wrap.

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

The code as it stands does not work - the new button gets added to the end of the column, and I want it to remain in the middle, where label2 was to begin with. What can I do, preferably in wrap_in_button, to make sure it ends up in the correct place? I'd rather keep it general, since the parent may be a Box or a Table or any general 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()

推荐答案

不是将标签直接放入主容器中,您可以将每个标签放入自己的盒子中.

Instead of putting the labels directly into the main container you can put each one into it's own 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)

为此:

box1 = gtk.HBox()
label1 = gtk.Label("Label 1")
box1.pack_start(label1)

box2 = gtk.HBox()
label2 = gtk.Label("Label 2")
box2.pack_start(label2)

box3 = gtk.HBox()
label3 = gtk.Label("Label 3")
box3.pack_start(label3)

box.pack_start(box1)
box.pack_start(box2)
box.pack_start(box3)

其余代码可以保持不变.您只需要确保这些框中一次只有 1 个子小部件.

The rest of the code can stay the same. You just have to make sure that you only have 1 child widget in those boxes at a time.

这篇关于PyGTK 小部件的就地替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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