python - 如何将 gobject.Gtype 参数传递给函数中的 Gtk.ListStore

标签 python gtk3

我想将类型和数据列表传递给函数来注册 Gtk.ListStore 对象。 它适用于单一类型,直接作为参数传递,但一旦有多种类型,就必须使用容器(列表?)。在这种情况下,我不知道如何将这些类型(str、int等) rebase 为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[:])

关于python - 如何将 gobject.Gtype 参数传递给函数中的 Gtk.ListStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370743/

相关文章:

linux - Genie 中的 Button.connect 语法

c - GDK3/GTK3 窗口更新的精确计时

css - 有没有办法通过css缩放GtkImage显示?

python - numpy 中多维数组的三角索引

Python Selenium 通过 data-filter-tag 值点击输入标签

python - 线性回归 ODR 失败

python - 如何在pytorch中进行渐变裁剪?

gotk3,带有自定义小部件的笔记本附加页面并将其取回

c - GTK 无法识别按键释放事件

python - 使用Python获取逻辑字符串中 "AND"中的元素