GtkWindow一次只能包含一个小部件 [英] GtkWindow can only contain one widget at a time
问题描述
class显示器(对象):$ b我使用这段代码从网上检索并显示图片:
$ b def __init __(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect('destroy',self.destroy)
self.window.set_border_width(10)
self.image = gtk.Image()
response = urllib2.urlopen('http://image.url/image.jpg')。 read()
pbuf = gtk.gdk.PixbufLoader()
pbuf.write(response)
pbuf.close()
self.image.set_from_pixbuf(pbuf .get_pixbuf())
self.window.add(self.image)
self.image.show()
self.window.show()
def main(self):
gtk.main()
def destroy(self,widget,data = None):
gtk.main_quit()
它可以工作,但是我现在想要在图像下方显示文本/输入框(以检索文本lat呃)。我在 self.image.show()
中添加了以下内容:
self.entry = gtk.Entry()
$ p $但是,它会吐出这个警告,然后我运行它,并且输入框不会出现:
self.window.add(self.entry)
self.entry.show()
ee.py:31:GtkWarning:试图将一个GtkEntry类型的控件添加到GtkWindow中,但是作为一个GtkBin子类,GtkWindow一次只能包含一个控件;它已经包含一个类型为GtkImage的小部件
self.window.add(self.entry)
不知道为什么它赢了让我放置多个小部件,有没有人有解决方案? 解决方案
确实打包就是答案。 / p>
导入gtk
导入urllib2
class显示(对象):
def __init __(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect('destroy',self.destroy)
self.window.set_border_width(10 )
#下面的盒子会在每次添加时加入
#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(r ())
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 =无):
gtk.main_quit()
a = Display()
a.main()
I am using this code to retrieve and display an image from the web:
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)
self.image = gtk.Image()
response = urllib2.urlopen('http://image.url/image.jpg').read()
pbuf = gtk.gdk.PixbufLoader()
pbuf.write(response)
pbuf.close()
self.image.set_from_pixbuf(pbuf.get_pixbuf())
self.window.add(self.image)
self.image.show()
self.window.show()
def main(self):
gtk.main()
def destroy(self, widget, data=None):
gtk.main_quit()
It works, however I now want to display a text/entry box underneath the image (to retrieve the text later on). I added the following under self.image.show()
:
self.entry = gtk.Entry()
self.window.add(self.entry)
self.entry.show()
However, it spits out this warning then I run it, and the entry box doesn't appear:
ee.py:31: GtkWarning: Attempting to add a widget with type GtkEntry to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkImage self.window.add(self.entry)
Not sure why it won't let me place more than one widget, does anyone have a solution for this?
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()
这篇关于GtkWindow一次只能包含一个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!