我想写一个函数,把字典转换成Gtk列表存储,其中Gtklist存储应该有n列,第一列是字典的键和值,其余的是空字符串。N应该由用户给出。在
ListStore的构造函数要求列的类型。如何在正确的数字中指定它们?在
以下是仅支持2或3列的函数来演示问题:
def dict2ListStore(dic, size=2):
if size == 2:
liststore = Gtk.ListStore(str, str)
for i in dic.items():
liststore.append(i)
return liststore
elif size == 3:
liststore = Gtk.ListStore(str, str, str)
for i in dic.items():
l = list(i)
l.append("")
liststore.append(l)
return liststore
else:
print("Error!")
return
[str] * size
是一个重复size
的列表。在func(*args)
是将序列args
中包含的值作为多个参数传递的方法。在相关问题 更多 >
编程相关推荐