2

这效果不好:

    image_log = gtk.Image()
    image_log.set_from_file("test.png")

    self.out_button = gtk.Button()
    self.out_button.add(image_log)

    self.err_button = gtk.Button()
    self.err_button.add(image_log)

    another_box.pack_start(self.out_button, False)
    another_box.pack_start(self.err_button, False)

问题是,image_log 被使用了两次,而 GTK 不喜欢它。有一些 .copy() 方法吗?还是我应该只使用普通的深拷贝?

编辑:看起来在 GTK 中没有克隆对象的默认方法。在这种情况下,工厂会解决问题。

GTK警告:

app/gui.py:248: GtkWarning: gtk_box_pack: assertion `child->parent == NULL' failed
hbox_errlog.pack_start(image_log)
4

4 回答 4

2

您可以使用工厂函数来减少代码重复

def make_image_from_file(fname):
  im = gtk.Image()
  im.set_from_file(fname)
  return im

self.out_button.set_image(make_image_from_file(..))

重访

还有一种更自然的方式。你会喜欢它。在 PyGTK 2.12+ 中:

gtk.image_new_from_file(filename)

我的脑海里有件事告诉我这件事,但我没有去查。

http://www.pygtk.org/docs/pygtk/class-gtkimage.html#function-gtk--image-new-from-file

于 2009-08-24T10:59:29.750 回答
2

采用

def clone_widget(widget):
    widget2=widget.__class__()
    for prop in dir(widget):
        if prop.startswith("set_") and prop not in ["set_buffer"]:
            prop_value=None
                try:
                    prop_value=getattr(widget, prop.replace("set_","get_") )()
                except:
                    try:
                        prop_value=getattr(widget, prop.replace("set_","") )
                    except:
                        continue
                if prop_value == None:
                    continue
                try:
                    getattr(widget2, prop)( prop_value ) 
                except:
                    pass
return widget2

所有这些尝试...除了块之外,因为并非所有属性都可以使用 set_prop(get_prop) 复制。我还没有对所有属性和小部件进行测试,但它对 gtkEntry 效果很好。也许这很慢,但很好用:)

于 2011-05-14T15:53:37.627 回答
1

为什么不

image_log = gtk.Image()
image_log.set_from_file("test.png")
image_logb = gtk.Image()
image_logb.set_from_file("test.png")

self.out_button = gtk.Button()
self.out_button.add(image_log)

self.err_button = gtk.Button()
self.err_button.add(image_logb)

another_box.pack_start(self.out_button, False)
another_box.pack_start(self.err_button, False)

它只是额外的 2 行代码,可能比克隆/复制第一个图像对象更有效。

这样你就可以out_button独立治疗err_button。但是为两个按钮使用相同的对象应该是有意义的gtk.Image()……它只是一个图像。

编辑 为了避免重复(虽然看起来有点矫枉过正),您可以为来自同一图像的 gtk.Image() 对象编写一个工厂。

def gtkimage_factory(num_objs, image_file):
    i=0
    imglist = []
    while i<num_objs:
        img_ob = gtk.Image()
        img_ob.set_from_file(image_file)
        imglist.append( img_ob )
        i+=1
    return imglist

或者类似的东西,你明白了。但是,除非您正在生产大量这些东西并且需要它们独立地在 GTK 中作为父对象,否则工厂似乎有点矫枉过正。然后...

image_list = gtkimg_factory(2, "test.png")

self.out_button = gtk.Button()
self.out_button.add(image_list[0])

self.err_button = gtk.Button()
self.err_button.add(image_list[1])

another_box.pack_start(self.out_button, False)
another_box.pack_start(self.err_button, False)

也许这与 GTK 资源管理有关?

于 2009-08-24T10:50:49.113 回答
0

如果您想一次又一次地使用以某种方式排列的小部件集合,假设一个带有一个输入框和一个标签的框(如果您愿意,可以很复杂)并且想在您的应用程序中多次使用它就像取决于条件需要多少个类似的选项卡,但显然使用的是使用带空地的复合材料的不同数据。使用 pygi(gi_composites) python 库,您可以制作自己的小部件并多次使用它们。[https://github.com/virtuald/pygi-composite-templates][1]

于 2021-01-04T13:06:01.173 回答