将元素添加到TreeView / ListStore后,应用程序崩溃

时间:2013-05-16 15:15:01

标签: python gtk pygtk

我的程序在添加少量文件时工作正常但在文件列表> 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的任何建议。

1 个答案:

答案 0 :(得分:1)

在函数_gdk_x11_display_error_event中从gdkdisplay-x11.c触发此错误。 这是由pygtk引起的吗?请试试gi模块。我用python3和gi进行测试,将超过500个项目附加到liststore时没有问题:

#!/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')