提问人:mario 提问时间:6/29/2010 最后编辑:Communitymario 更新时间:3/7/2011 访问量:3081
更高级别的 Python GUI 工具包,例如 TreeView/Grid 的 pass dict
higher level Python GUI toolkit, e.g. pass dict for TreeView/Grid
问:
使用 PyGTK 开始了我的第一个 Python 宠物项目。虽然它是一个非常强大的 GUI 工具包并且看起来很棒,但我有一些小烦恼。所以我考虑过渡到其他东西,因为它还不是太广泛。浏览了一下 SO 和 python 文档,但没有得到很好的概述。
PyGTK有什么好处:
- 格莱德文件
- self.signal_autoconnect({...})
- self.get_widget() 作为 __getattr__
然而,这让我很烦恼:
- 手动gobject.idle_add(lambda:...和 False)
- 没有保存应用程序/窗口状态的标准功能
- TreeView 需要构建数组
- widget.get_selection().get_selected(), model.get_value(iter, liststore_index)
TreeView:因为这是主界面元素,所以它最容易分散注意力。基本上,我的应用程序构建了一个字典列表,以显示 name=column+row=>value。要使用 GTK 显示它,需要有一个手动转换过程、排序、类型转换。这似乎有很多开销,我希望在这里有更多的面向对象的东西。PyGtk 在 gtk+ 之上有许多抽象,但看起来仍然相当低级。我更愿意按原样传递我的字典,并以某种方式预定义列。(GtkBuilder可以预定义TreeView列,但这并不能解决数据表示开销。
当我在 TreeView 列表中单击鼠标时,我还必须将所有内容转换回我的应用程序数据结构。同样令人讨厌的是,如果从非主线程运行,PyGTK 不会使用 gobject.idle 本身包装 gtk+ 调用。现在有很多 GUI 代码我认为不应该是必要的,或者可以合理化。
?那么,在PyGTK之上是否有额外的包装器。或者哪个其他工具包支持用于显示 Grid/TreeView 的更简单的接口。我读过很多关于wxPython是每个人的最爱的文章,但它在Linux上还不太成熟。PyQT 似乎与 PyGTK 的抽象级别基本相同。没怎么使用过 TkInter,所以不知道它是否有更简单的界面,但它看起来没有吸引力。PyFLTK 也是如此。PyJamas听起来很吸引人,但已经太遥远了(桌面应用程序)。
.
因此,带有 dict -> Grid 显示的 GUI 工具包。你会选择哪一个?
.
就像展览一样,这是我当前的 TreeView 映射函数。有点工作,但我宁愿有一些标准的东西:
#-- fill a treeview
#
# Adds treeviewcolumns/cellrenderers and liststore from a data dictionary.
# Its datamap and the table contents can be supplied in one or two steps.
# When new data gets applied, the columns aren't recreated.
#
# The columns are created according to the datamap, which describes cell
# mapping and layout. Columns can have multiple cellrenderers, but usually
# there is a direct mapping to a data source key from entries.
#
# datamap = [ # title width dict-key type, renderer, attrs
# ["Name", 150, ["titlerow", str, "text", {} ] ],
# [False, 0, ["interndat", int, None, {} ] ],
# ["Desc", 200, ["descriptn", str, "text", {} ], ["icon",str,"pixbuf",{}] ],
#
# An according entries list then would contain a dictionary for each row:
# entries = [ {"titlerow":"first", "interndat":123}, {"titlerow":"..."}, ]
# Keys not mentioned in the datamap get ignored, and defaults are applied
# for missing cols. All values must already be in the correct type however.
#
@staticmethod
def columns(widget, datamap=[], entries=[], pix_entry=False):
# create treeviewcolumns?
if (not widget.get_column(0)):
# loop through titles
datapos = 0
for n_col,desc in enumerate(datamap):
# check for title
if (type(desc[0]) != str):
datapos += 1 # if there is none, this is just an undisplayed data column
continue
# new tvcolumn
col = gtk.TreeViewColumn(desc[0]) # title
col.set_resizable(True)
# width
if (desc[1] > 0):
col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
col.set_fixed_width(desc[1])
# loop through cells
for var in xrange(2, len(desc)):
cell = desc[var]
# cell renderer
if (cell[2] == "pixbuf"):
rend = gtk.CellRendererPixbuf() # img cell
if (cell[1] == str):
cell[3]["stock_id"] = datapos # for stock icons
expand = False
else:
pix_entry = datapos
cell[3]["pixbuf"] = datapos
else:
rend = gtk.CellRendererText() # text cell
cell[3]["text"] = datapos
col.set_sort_column_id(datapos) # only on textual cells
# attach cell to column
col.pack_end(rend, expand=cell[3].get("expand",True))
# apply attributes
for attr,val in cell[3].iteritems():
col.add_attribute(rend, attr, val)
# next
datapos += 1
# add column to treeview
widget.append_column(col)
# finalize widget
widget.set_search_column(2) #??
widget.set_reorderable(True)
# add data?
if (entries):
#- expand datamap
vartypes = [] #(str, str, bool, str, int, int, gtk.gdk.Pixbuf, str, int)
rowmap = [] #["title", "desc", "bookmarked", "name", "count", "max", "img", ...]
if (not rowmap):
for desc in datamap:
for var in xrange(2, len(desc)):
vartypes.append(desc[var][3]) # content types
rowmap.append(desc[var][0]) # dict{} column keys in entries[] list
# create gtk array storage
ls = gtk.ListStore(*vartypes) # could be a TreeStore, too
# prepare for missing values, and special variable types
defaults = {
str: "",
unicode: u"",
bool: False,
int: 0,
gtk.gdk.Pixbuf: gtk.gdk.pixbuf_new_from_data("\0\0\0\0",gtk.gdk.COLORSPACE_RGB,True,8,1,1,4)
}
if gtk.gdk.Pixbuf in vartypes:
pix_entry = vartypes.index(gtk.gdk.Pixbuf)
# sort data into gtk liststore array
for row in entries:
# generate ordered list from dictionary, using rowmap association
row = [ row.get( skey , defaults[vartypes[i]] ) for i,skey in enumerate(rowmap) ]
# autotransform string -> gtk image object
if (pix_entry and type(row[pix_entry]) == str):
row[pix_entry] = gtk.gdk.pixbuf_new_from_file(row[pix_entry])
# add
ls.append(row) # had to be adapted for real TreeStore (would require additional input for grouping/level/parents)
# apply array to widget
widget.set_model(ls)
return ls
pass
答:
我建议看看wxPython。我发现它真的很容易上手,而且非常强大,尽管我不得不承认我自己并没有用 Treeviews 做很多事情。
wxWidgets 调用等效控件 wxTreeCtrl
[编辑]在您的情况下,wxDataViewTreeCtrl 实际上可能更有用。
评论
试试猕猴桃吧?尤其是它的 ObjectList。
更新:我认为 Kiwi 开发已经转移到 PyGTKHelpers。
评论
我以前没有遇到过猕猴桃。谢谢,约翰内斯·萨松科。
以下是我保留书签的更多 tooklits。其中一些是其他工具包(GTK、wxWidgets)的包装器,而另一些则是独立的:
(为了其他遇到这篇文章的人,我包括了一些已经提到的内容。我本来会把它作为评论发布,但它有点太长了。
评论
上一个:论坛/板块写在大型PHP框架之上
下一个:复杂类型作为数组索引
评论