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

夙愿已清 提交于 2020-01-14 13:32:13

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/6125902/in-pygtk-what-is-a-simple-way-to-show-a-png-file

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