First the code:
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()
Every time I do .append() on ListStore object I get such error:
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
Of course the ListStore object is (str, str, str, boolean) type. The error message is ridiculous, can anyone tell what's going on?
The problem was I used python type names, instead of GTK ones, while defining types of columns. I set it to (str, str, str, bool), but it should be (gchararray, gchararray, gchararray, gboolean).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With