Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Gtk3和python添加图标时的核心转储_Python 3.x_Gtk3_Pygobject - Fatal编程技术网

Python 3.x 使用Gtk3和python添加图标时的核心转储

Python 3.x 使用Gtk3和python添加图标时的核心转储,python-3.x,gtk3,pygobject,Python 3.x,Gtk3,Pygobject,我正在使用GTK3(来自gi.repository)和python3创建一个UI。当我向UI添加默认图标,然后运行程序时,它会崩溃,并出现以下错误: 分段错误(核心转储)python main.py 我正在使用Gtk的设置图标列表方法添加图标。窗口: self.c\u win.set\u图标列表(图标列表) 如果我对这行进行注释,程序将按预期运行。我通过以下功能获得图标列表: def load_icon(): req = pkg_resources.Requirement.parse("

我正在使用GTK3(来自gi.repository)和python3创建一个UI。当我向UI添加默认图标,然后运行程序时,它会崩溃,并出现以下错误:

分段错误(核心转储)python main.py

我正在使用Gtk的
设置图标列表
方法添加图标。窗口:

self.c\u win.set\u图标列表(图标列表)

如果我对这行进行注释,程序将按预期运行。我通过以下功能获得图标列表:

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_图标_列表的预期输入


github上提供了完整的代码:知道问题出在哪里吗?

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

你真正想要的是:

icon_pixbuf = Pixbuf.new_from_file(icon_fn)
icons.append(icon_pixbuf)
无论如何,它都不应该出现故障。请将其记录为bug,并提供导致崩溃的最少代码示例:

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

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

它解决了这个问题。谢谢我在这里打开一个bug报告:
import gi
from gi.repository import Gtk
print(gi.version_info)
print(Gtk.MINOR_VERSION)