python treeview显示多列_Python Gtk TreeView列数据显示

本文档描述了在Python GTK中使用TreeView显示多列数据时遇到的问题。作者发现当通过循环插入列时,所有后续列都显示了第一列的数据,而非预期的ListStore中的对应数据。为了解决这个问题,作者提供了代码示例,展示了如何正确设置CellRendererText和TreeViewColumn以显示正确的数据,并尝试用循环实现,但遇到了列数据重复的问题。
摘要由CSDN通过智能技术生成

我一直在使用这里找到的代码:

几年来成功地格式化了Treeview数字列。但是当我使用循环插入列时,列显示的是第一列中的数据,而不是我希望从ListStore中获取的数据。为什么会这样?我已经为此挣扎了一段时间了,这可能是一个非常简单的解决方案,但我相当无知!!非常感谢你。下面是一个可以说明我的问题的有效示例:from gi.repository import Gtk, Gdk

class myClass:

def __init__(self):

# Setup

self.iListstore = Gtk.ListStore(str, float, float, float, float)

self.iListstore.append(['abc',209.8967,568.56432, 1, 2])

self.iListstore.append(['def',2409.846,559.534, 3, 4])

self.window = Gtk.Window()

self.iTreeView = Gtk.TreeView(self.iListstore)

# Column 0

lblr= Gtk.CellRendererText()

lcol = Gtk.TreeViewColumn('Row Label')

self.iTreeView.append_column(lcol)

lcol.pack_start(lblr, True)

lcol.add_attribute(lblr, 'text',0)

# Column 1

cr = Gtk.CellRendererText(xalign=1)

myCol = Gtk.TreeViewColumn('Col1')

myCol.set_sort_column_id(1)

self.iTreeView.append_column(myCol)

myCol.pack_start(cr, True)

myCol.add_attribute(cr, 'text',1)

myCol.set_cell_data_func(cr,lambda column, cell, model, iter, unused:cell.set_property("text","{0:.2f}".format(round(model.get(iter,1)[0],2))))

# Column 2

myCol = Gtk.TreeViewColumn('Col2')

myCol.set_sort_column_id(2)

self.iTreeView.append_column(myCol)

myCol.pack_start(cr, True)

myCol.add_attribute(cr, 'text',2)

myCol.set_cell_data_func(cr,lambda column, cell, model, iter, unused:cell.set_property("text","{0:.2f}".format(round(model.get(iter,2)[0],2))))

# The above works but the following does not. Col3 has the same value as Col4. Can someone tell me the reason a loop can not be used with the code?

colNames=['Col3','Col4']

for i in range(3,5):

myCol = Gtk.TreeViewColumn(colNames[i-3]) # I realize this is a bit of a fudge

myCol.set_sort_column_id(i)

self.iTreeView.append_column(myCol)

myCol.pack_start(cr, True)

myCol.add_attribute(cr, 'text',i)

myCol.set_cell_data_func(cr,lambda column, cell, model, iter, unused:cell.set_property("text","{0:.2f}".format(round(model.get(iter,i)[0],2))))

# Window

self.window.add(self.iTreeView)

self.window.show_all()

def main(self):

Gtk.main()

p=myClass()

p.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值