python - 在 PyGTK 中,显示 PNG 文件的简单方法是什么?

标签 python gtk png pygtk gtk2

以下 PyGTK 代码在窗口中显示 PNG 文件。

是否有更简单或更好的显示 PNG 文件的方法,例如使用 gtk.DrawingArea?例如,如何调整文件大小?

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()

致谢:我使用网络上其他人的代码创建了上述代码。

最佳答案

你应该直接打电话image.set_from_file而不是创建一个 pixbuf 和一个 pixmap。至于处理调整大小,我直接使用 gtk.DrawingArea,使用 configure 信号获取绘图区域的高度和宽度,以及 expose-event 在整个表面上用 cairo 绘制(paint)的事件。但也可能有一种使用 gtk.Image 的方法。

关于python - 在 PyGTK 中,显示 PNG 文件的简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125902/

相关文章:

python - 使用 GTK TextView 撤消

ubuntu - GtkDialog 映射时没有 transient 父级。这是不鼓励的,Katalon Studio Linux (Beta GUI) 出现错误

css - 这可能吗?单击 iframe 上的图像

delphi - TWICImage : How to overlay transparent PNG on JPG?

python - 创建新的 pandas 行,作为来自不同行的文本值组合的结果,这些文本值在其他 pandas 列中具有相同的值

python - win32com - 在 Excel 工作表中写入字符串值

c++ - WebKit2GTK+ "page-created"扩展只能工作一次

python - 带有 entry_points 的设置工具

Python将colorsys RGB坐标转换为十六进制

c# - 无法在运行 Mono 的 Mac OSX 上以 C# 加载 PNG