在PyGTK中,显示PNG文件的简单方法是什么? [英] In PyGTK, what is a simple way to show a PNG file?
问题描述
以下PyGTK代码在窗口中显示PNG文件.
The following PyGTK code displays a PNG file in a window.
是否有一种更简单或更佳的显示PNG文件的方式,例如使用gtk.DrawingArea?例如,如何调整文件大小?
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.
这篇关于在PyGTK中,显示PNG文件的简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!