【问题标题】:How do I include an image in a window with pygtk?如何使用 pygtk 在窗口中包含图像?
【发布时间】:2010-09-12 15:27:24
【问题描述】:

我正在尝试在 python 中创建一个程序,它创建一个全屏窗口并包含一个图像,但我真的不知道该怎么做。我尝试阅读有关 pygtk 的文档,并且在 goodle 和 stackoverflow 中都进行了搜索,但均未成功。 这是我当前的代码。

def __init__(self):
    pixbuf = gtk.gdk.pixbuf_new_from_file("test.png")
    image = gtk.Image()
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.fullscreen()
    self.window.show()
    image.set_from_pixbuf(pixbuf)
    image.show()

问题:如何在窗口中包含图像?

【问题讨论】:

    标签: python image gtk pygtk


    【解决方案1】:

    请提供更多上下文(例如类定义、导入)。

    不要忘记将image 对象添加到您的窗口(在显示图像和窗口之前):

    self.window.add(image)
    

    tutorial example 将图像添加到button,但您可以尝试将其直接添加到主窗口:

    # an image widget to contain the pixmap
    image = gtk.Image()
    image.set_from_pixmap(pixmap, mask)
    image.show()
    
    # a button to contain the image widget
    button = gtk.Button()
    button.add(image)
    window.add(button)
    button.show()
    
    button.connect("clicked", self.button_clicked)
    

    【讨论】:

    • 谢谢,它就像一个魅力。如果你愿意,你可以看到整个代码here.
    • 作为问题一部分的一点上下文有助于理解。很高兴提供解决方案 ;-)
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多