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

发布于 2024-08-03 04:38:08 字数 658 浏览 2 评论 0原文

这效果不太好:

    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)

This doesn't work well:

    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)

The problem is, image_log is used twice and GTK doesn't like it. Is there some .copy() method? Or should I just use plain vanilla deepcopy?

EDIT: Looks like there is no default way to clone objects in GTK. Factory will do the trick in this case.

GTK warning:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃→可爱长大的 2024-08-10 04:38:08

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

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

You could use a factory function to reduce code duplication

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

Revisiting

There is a much more natural way. You will like it. In PyGTK 2.12+:

gtk.image_new_from_file(filename)

I had something in the back of my mind telling me this, but I didn't look it up.

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

如何视而不见 2024-08-10 04:38:08

使用

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

所有这些 try ... except 块都在那里,因为并非所有属性都可以通过使用复制
set_prop(get_prop).我还没有对所有属性和小部件进行测试,但它对于 gtkEntry 效果很好。也许这很慢,但使用起来很好:)

Use

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

All this try ... except blocks are there because not all properties could be copied by using
set_prop(get_prop). I haven't tested this for all properties and widgets yet, but it worked well for gtkEntry. Maybe this is slow, but it's nice the use :)

彼岸花ソ最美的依靠 2024-08-10 04:38:08

为什么不呢?

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_buttonerr_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资源管理有关?

Why not

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)

It is only an extra 2 lines of code, and maybe more efficient than cloning/copying the first image object.

That way you can treat out_button and err_button independently. But it should make sense to use the same gtk.Image() object for both buttons ... it is just an image.

Edit
To avoid duplication (seems like overkill though) you could write a factory for gtk.Image() objects from the same 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

Or something along those lines, you get the idea. But a factory seems like overkill unless you are producing loads of these things and need them independently parented in GTK.
Then...

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)

Maybe it is something to do with GTK resource management?

我的奇迹 2024-08-10 04:38:08

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

all if you want to use the collection of widgets arranged in some fashion, again and again, let's say a box with one entry box and a label(can be complex as hell if you want) and want to use it multiple time in your application like depending upon condition how many similar tabs are needed but obviously working with different data that use composites with glade.
With pygi(gi_composites) python library you can make your own widgets and use them multiple times.
[https://github.com/virtuald/pygi-composite-templates][1]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文