-1

我的程序在添加少量文件时工作正常,但在文件列表 > 500 时崩溃。

错误:

The program 'gui.py' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAlloc (insufficient resources for operation)'.
  (Details: serial 14768 error_code 11 request_code 53 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

编码:

self.tf_idf_document_list = gtk.ListStore(str)
self.tf_idf_tree_documents.set_model(self.tf_idf_document_list)
self.tf_idf_tree_column.set_attributes(self.tf_idf_renderer, text=0)

在 FileChooser 上,我选择包含文件的文件夹并将它们添加到列表中:

    os.chdir(dir_name)   
    for file_name in os.listdir("."):
        self.tf_idf_document_list.append([file_name])

任何关于如何解决这种 BoF 的建议。

4

1 回答 1

1

此错误由函数 _gdk_x11_display_error_event 中的 gdkdisplay-x11.c 触发。这是由pygtk引起的吗?请尝试 gi 模块。我已经用 python3 和 gi 进行了测试,将 500 多个项目附加到列表存储时没有问题:

#!/usr/bin/env python3

from gi.repository import Gtk
import os


def main(dir_name='.'):
    win = Gtk.Window()
    win.set_default_size(300, 400)
    win.connect('destroy', Gtk.main_quit)

    liststore = Gtk.ListStore(str)
    i = 0
    for file_name in os.listdir(dir_name):
        i += 1
        liststore.append([file_name])
    for file_name in os.listdir(dir_name):
        i += 1
        liststore.append([file_name])
    print('num of items:', i)

    treeview = Gtk.TreeView()
    treeview.set_model(liststore)
    renderer_text = Gtk.CellRendererText()
    column_text = Gtk.TreeViewColumn('File', renderer_text, text=0)
    treeview.append_column(column_text)

    scrolledwindow = Gtk.ScrolledWindow()
    scrolledwindow.add(treeview)
    win.add(scrolledwindow)

    win.show_all()
    Gtk.main()

if __name__ == '__main__':
    main('/usr/share')
于 2013-05-16T16:27:03.787 回答