如何将 gobject.Gtype 参数传递给函数中的 Gtk.ListStore
How to pass gobject.Gtype arguments to a Gtk.ListStore in a function
我想将类型和数据列表传递给函数以注册 Gtk.ListStore 对象。
它使用单一类型,直接作为参数传递,但一旦有多种类型,就需要使用容器(列表?)。在这种情况下,我不知道如何将这些类型(str、int 等)变基为 gobject.Gtype。
如果有人有解决方案那就太好了。
下面的示例代码。
提前致谢。
# Fonctions
def initListStore(lesTypes, laListe):
leStore = Gtk.ListStore(gobject.Gtype(lesTypes))
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lesTypes = 'str str'
lsAuteurs = initListStore(lesTypes,liste)
for row in lsAuteurs:
print (row[:])
这是一种可能的解决方案:
from gi.repository import Gtk
def initListStore(lesTypes, laListe):
leStore = Gtk.ListStore()
leStore.set_column_types(lesTypes)
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lesTypes = (str, str)
lsAuteurs = initListStore(lesTypes,liste)
for row in lsAuteurs:
print (row[:])
最后我改进了函数,将listStore的管理完全外包给函数本身,它现在只接收一个参数:数据列表。非常感谢 theGtknerd 解锁了我。
from gi.repository import Gtk
def initListStore(laListe):
types = []
ligne = laListe[0]
for mot in ligne:
types.append(type(mot))
leStore = Gtk.ListStore()
leStore.set_column_types(types)
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lsAuteurs = initListStore(liste)
for row in lsAuteurs:
print (row[:])
我想将类型和数据列表传递给函数以注册 Gtk.ListStore 对象。 它使用单一类型,直接作为参数传递,但一旦有多种类型,就需要使用容器(列表?)。在这种情况下,我不知道如何将这些类型(str、int 等)变基为 gobject.Gtype。 如果有人有解决方案那就太好了。 下面的示例代码。
提前致谢。
# Fonctions
def initListStore(lesTypes, laListe):
leStore = Gtk.ListStore(gobject.Gtype(lesTypes))
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lesTypes = 'str str'
lsAuteurs = initListStore(lesTypes,liste)
for row in lsAuteurs:
print (row[:])
这是一种可能的解决方案:
from gi.repository import Gtk
def initListStore(lesTypes, laListe):
leStore = Gtk.ListStore()
leStore.set_column_types(lesTypes)
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lesTypes = (str, str)
lsAuteurs = initListStore(lesTypes,liste)
for row in lsAuteurs:
print (row[:])
最后我改进了函数,将listStore的管理完全外包给函数本身,它现在只接收一个参数:数据列表。非常感谢 theGtknerd 解锁了我。
from gi.repository import Gtk
def initListStore(laListe):
types = []
ligne = laListe[0]
for mot in ligne:
types.append(type(mot))
leStore = Gtk.ListStore()
leStore.set_column_types(types)
for item in laListe:
leStore.append(item)
return leStore
# Appli
liste = [('Albert','Einstein'),('Salvador','Dali'),('Alexandre','Dumas')]
lsAuteurs = initListStore(liste)
for row in lsAuteurs:
print (row[:])