【问题标题】:Core dump when adding icons with Gtk3 and python使用 Gtk3 和 python 添加图标时的核心转储
【发布时间】:2014-08-02 17:05:36
【问题描述】:

我正在使用 GTK3(来自 gi.repository)和 python3 创建一个 UI。当我将默认图标添加到 UI 并运行程序时,它会因同伴错误而崩溃:

segmentation fault (core dumped) python main.py

我正在使用 Gtk.Window 的set_icon_list 方法添加图标:

self.c_win.set_icon_list(icon_list)

如果我评论这一行,程序会按预期运行。我得到具有以下功能的图标列表:

def load_icon():
    req = pkg_resources.Requirement.parse("pympress")

   # If pkg_resources fails, load from directory
   try:
       icon_names = pkg_resources.resource_listdir(req, "share/pixmaps")
    except pkg_resources.DistributionNotFound:
       icon_names = os.listdir("share/pixmaps")
    icons = []
    for icon_name in icon_names:
       if os.path.splitext(icon_name)[1].lower() != ".png":
           continue

        # If pkg_resources fails, load from directory
        try:
            icon_fn = pkg_resources.resource_filename(req, "share/pixmaps/{}".format(icon_name))
        except pkg_resources.DistributionNotFound:
            icon_fn = "share/pixmaps/{}".format(icon_name)
        try:
            icon_pixbuf = Pixbuf()
            icon_pixbuf.new_from_file(icon_fn)
            icons.append(icon_pixbuf)
        except Exception as e:
            print(e)
    return icons

它返回一个 Pixbuf 列表,它是 set_icon_list 的预期输入。

完整代码可在 github 上找到:https://github.com/Jenselme/pympress 知道问题出在哪里吗?

【问题讨论】:

    标签: python-3.x gtk3 pygobject


    【解决方案1】:

    虽然它不应该崩溃,但部分问题可能是由于使用 new_from_file() 的方式。 new_from_file() 是一个构造函数,它返回一个 new pixbuf,您应该将它存储在一个变量中。它不会将文件的内容加载到现有的 pixbuf 中。所以“图标”列表实际上包含一堆空的(或者更确切地说是 1x1)像素缓冲区。

    # Creates a new 1x1 pixbuf.
    icon_pixbuf = Pixbuf()
    
    # Creates a new pixbuf from the file the value of which is lost
    # because there is no assignment.
    icon_pixbuf.new_from_file(icon_fn)
    
    # Stores the first 1x1 pixbuf in the list.
    icons.append(icon_pixbuf)
    

    你真正想要的是:

    icon_pixbuf = Pixbuf.new_from_file(icon_fn)
    icons.append(icon_pixbuf)
    

    在任何情况下,它都不应该出现段错误。请使用导致崩溃的最小代码示例将其记录为错误: https://bugzilla.gnome.org/enter_bug.cgi?product=pygobject

    还要注意使用的 gi 和 GTK+ 的版本:

    import gi
    from gi.repository import Gtk
    print(gi.version_info)
    print(Gtk.MINOR_VERSION)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多