gpt4 book ai didi

python - 更高级别的 Python GUI 工具包,例如为 TreeView/Grid 传递字典

转载 作者:太空狗 更新时间:2023-10-30 00:30:40 27 4
gpt4 key购买 nike

使用 PyGTK 开始了我的第一个 Python 宠物项目。虽然它是一个非常强大的 GUI 工具包并且看起来很棒,但我还是有些不满。所以我考虑过渡到其他东西,因为它还不太广泛。环顾四周 SOpython documentation , 但没有得到很好的概述。

PyGTK 的优点:

  • 空地文件
  • self.signal_autoconnect({...})
  • self.get_widget() 作为 __getattr__

然而,这让我很烦恼:

  • 手动 gobject.idle_add(lambda: ... and 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 一样。睡衣听起来很迷人,但已经太遥远了(桌面应用程序)。

.

因此,带有字典的 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

最佳答案

尝试 Kiwi , 或许?特别是它的 ObjectList .

更新:我认为 Kiwi 开发已移至 PyGTKHelpers .

关于python - 更高级别的 Python GUI 工具包,例如为 TreeView/Grid 传递字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com