向ListStore添加一行不起作用 - 荒谬的异常

时间:2013-12-16 22:08:03

标签: python python-3.x gtk3 gobject-introspection

首先是代码:

from gi.repository import Gtk, GtkSource, GObject

import os.path
import shelve

class MusicCollection(object):

    def __init__(self):
        self.builder = Gtk.Builder()
        self.glade_file = 'music.glade'
        GObject.type_register(GtkSource.View)
        self.builder.add_from_file(self.glade_file)
        self.window = self.builder.get_object("window1")
        self.entry_author = self.builder.get_object("entry_author")
        self.entry_album = self.builder.get_object("entry_album")
        self.entry_songs = self.builder.get_object("entry_songs")
        self.borrowed_toggle = self.builder.get_object("borrowed")
        self.create_button = self.builder.get_object("create")
        self.update_button = self.builder.get_object("update")
        self.delete_button = self.builder.get_object("destroy")
        self.view = self.builder.get_object("treeview1")
        self.albums_list = self.builder.get_object("albums_list")

        self.db = shelve.open("data.db")
        self.columns = ["Author", "Album", "Songs", "Borrowed"]        

        self.connect_signals()
        self.initialize()
        self.window.show_all()

    def connect_signals(self):
        self.create_button.connect("clicked", self.create)
        self.update_button.connect("clicked", self.update)
        self.delete_button.connect("clicked", self.destroy)
        self.window.connect("destroy", self.quit)
        self.view.get_selection().connect("changed", self.changed)

    def initialize(self):
        try:
          self.albums = self.db['albums']
        except KeyError:
          self.db["albums"] = []
          self.db.sync()

        self.albums = self.db['albums']

        for i in range(len(self.albums)):
            self.albums_list.append(self.albums[i])

        for i in range(len(self.columns)):
            cell = Gtk.CellRendererText()
            col = Gtk.TreeViewColumn(self.columns[i], cell, text=i)
            self.view.append_column(col)

    def update(self, button):
        (instance, iterator) = self.view.get_selection().get_selected()
        newItem = (self.entry_author.get_text(),self.entry_album.get_text(),self.entry_songs.get_text(), self.borrowed_toggle.get_mode())
        i = self.albums_list.get_path(iterator)[0]
        self.albums.pop(i)
        self.albums.insert(i,newItem)
        instance[iterator][0] = self.entry_author.get_text()
        instance[iterator][1] = self.entry_album.get_text()
        instance[iterator][2] = self.entry_songs.get_text()
        instance[iterator][3] = self.borrowed_toggle.get_mode()
        self.db['albums'] = self.albums
        self.db.sync()


    def destroy(self, selection):
        (instance, iterator) = self.view.get_selection().get_selected()
        toDel = instance[iiterator][0]
        i = self.albums_list.get_path(iterator)[0]
        self.albums_list.remove(iterator)
        self.albums.pop(i)

        self.db['albums'] = self.albums
        self.db.sync()

    def create(self, somtin):
        new_entry = (self.entry_author.get_text(),self.entry_album.get_text(),self.entry_songs.get_text(), self.borrowed_toggle.get_mode())
        print(new_entry)
        self.albums.append(new_entry)
        self.db['albums'] = self.albums
        self.db.sync()
        self.albums_list.append(new_entry)


    def changed(self, selection):
        (instance, iterator) = selection.get_selected()
        self.entry_author.set_text(instance[iterator][0])
        self.entry_album.set_text(instance[iterator][1])
        self.entry_songs.set_text(instance[iterator][2])
        self.borrowedto.set_mode(instance[iterator][3])

    def quit(self, elem):
        self.db.close()
        Gtk.main_quit()

if __name__ == "__main__":
    collection = MusicCollection()
    Gtk.main()

每次我在ListStore对象上执行.append()时都会出现这样的错误:

Traceback (most recent call last):
  File "./music.py", line 85, in create
    self.albums_list.append(new_entry)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 945, in append
    return self._do_insert(-1, row)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 936, in _do_insert
    row, columns = self._convert_row(row)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 844, in _convert_row
    result.append(self._convert_value(cur_col, value))
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 862, in _convert_value
    return GObject.Value(self.get_column_type(column), value)
  File "/usr/lib/python3/dist-packages/gi/overrides/GObject.py", line 214, in __init__
    self.init(value_type)
TypeError: Must be gobject.GType, not gobject.GType

当然ListStore对象是(str,str,str,boolean)类型。错误信息很荒谬,任何人都能说出发生了什么事吗?

1 个答案:

答案 0 :(得分:5)

问题是我在定义列类型时使用了python类型名称而不是GTK名称。 我把它设置为(str,str,str,bool),但它应该是(gchararray,gchararray,gchararray,gboolean)。