GtkWindow can only contain one widget at a time

▼魔方 西西 提交于 2019-12-06 06:00:06

Indeed packing is the answer.

import gtk
import urllib2
class Display(object):

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect('destroy', self.destroy)
        self.window.set_border_width(10)

        # a box underneath would be added every time you do 
        # vbox.pack_start(new_widget)

        vbox = gtk.VBox()
        self.image = gtk.Image()
        response = urllib2.urlopen('http://1.bp.blogspot.com/-e-rzcjuCpk8/T3H-mSry7PI/AAAAAAAAOrc/Z3XrqSQNrSA/s1600/rubberDuck.jpg').read()

        pbuf = gtk.gdk.PixbufLoader()
        pbuf.write(response)
        pbuf.close()
        self.image.set_from_pixbuf(pbuf.get_pixbuf())

        self.window.add(vbox)
        vbox.pack_start(self.image, False)
        self.entry = gtk.Entry()
        vbox.pack_start(self.entry, False)

        self.image.show()
        self.window.show_all()

    def main(self):
        gtk.main()

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

a=Display()
a.main()

Take a look into widget packing. Essentially you use window.add to add a special packaging container, which in turn contains the main widgets and/or additional containers.

sketch:

hbox = HBox()
window.add(hbox)
hbox.pack_start(widget1)
hbox.pack_start(widget2)
window.show_all()

A GTK Window can only contain one child. If you want to add multiple widgets then you need a layout container such as a box or a grid to hold them. Boxes are fine in GTK2 but in GTK3 the developers advise switching to grids as boxes are now deprecated in GTK3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!