开发者

In PyGTK, what is a simple way to show a PNG file?

开发者 https://www.devze.com 2023-03-08 03:03 出处:网络
The following PyGTK code displays a PNG file in a window. 开发者_Python百科 Is there a simpler or better way of displaying the PNG file, like, by using a gtk.DrawingArea? For example, how do you resiz

The following PyGTK code displays a PNG file in a window.

开发者_Python百科

Is there a simpler or better way of displaying the PNG file, like, by using a gtk.DrawingArea? For example, how do you resize the file?

import gtk
import pygtk
pygtk.require('2.0')

class Gui:

    def __init__(self):

        # Create an Image object for a PNG file.
        file_name = "file.png"
        pixbuf = gtk.gdk.pixbuf_new_from_file(file_name)
        pixmap, mask = pixbuf.render_pixmap_and_mask()
        image = gtk.Image()
        image.set_from_pixmap(pixmap, mask)

        # Create a window.
        window = gtk.Window()
        window.set_title("PNG file")
        window.connect("destroy", gtk.main_quit)

        # Show the PNG file in the window.
        window.add(image)
        window.show_all()

if __name__ == "__main__":
    Gui()
    gtk.main()

Acknowledgments: I created the above code using code from other people on the web.


You should call directly image.set_from_file instead of creating a pixbuf and a pixmap. As to handle resizing, I did it once by using directly a gtk.DrawingArea, using the configure signal to get the height and width of the drawing area, and the expose-event event to paint it with cairo on the whole surface. But there may be a way of using a gtk.Image too.

0

精彩评论

暂无评论...
验证码 换一张
取 消