代码之家  ›  专栏  ›  技术社区  ›  iElectric

如何在Pygtk中使用同一个小部件两次?

  •  2
  • iElectric  · 技术社区  · 15 年前

    这不太管用:

        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)
    

    问题是,图像日志使用了两次,GTK不喜欢。有没有.copy()方法?或者我应该只使用普通香草味的迪普复制品吗?

    编辑: 似乎没有在GTK中克隆对象的默认方法。在这种情况下,工厂将采取相应的措施。

    GTK警告:

    app/gui.py:248: GtkWarning: gtk_box_pack: assertion `child->parent == NULL' failed
    hbox_errlog.pack_start(image_log)
    
    3 回复  |  直到 14 年前
        1
  •  2
  •   u0b34a0f6ae    15 年前

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

    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

        2
  •  2
  •   Anonymous    14 年前

    使用

    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
    

    所有这些尝试…因为不是所有的属性都可以使用 设置道具(获取道具)。我还没有为所有的属性和小部件测试过这个,但是它对GTKentry很有效。也许这很慢,但使用起来很好。)

        3
  •  1
  •   Aiden Bell    15 年前

    为什么不呢?

    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)
    

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

    这样你就可以治疗了 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资源管理有关?