gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:03:04 26 4
gpt4 key购买 nike

首先是代码:

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)类型。错误消息很荒谬,谁能告诉我发生了什么事?

最佳答案

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

关于python - 向 ListStore 添加行不起作用 - 荒谬的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com