Python: 调用具有不同数量参数的函数

2024-09-30 14:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个函数,把字典转换成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

Tags: 函数ingtk列表forsizereturn字典
1条回答
网友
1楼 · 发布于 2024-09-30 14:27:48
liststore = Gtk.ListStore(*([str] * size))

[str] * size是一个重复size的列表。在

func(*args)是将序列args中包含的值作为多个参数传递的方法。在

相关问题 更多 >