我执行了这个程序:
#!/usr/bin/env python
import gtk
class CellRenderer:
def __init__(self):
window = gtk.Window()
window.set_default_size(200,200)
liststore = gtk.ListStore(str, str)
liststore.append(["Fedora","http://fedoraproject.org"])
liststore.append(["Slackware","http://www.slackware.com"])
liststore.append(["Sidux","http://sidux.com"])
treeview = gtk.TreeView(liststore)
column_text = gtk.TreeViewColumn("Distributions")
column_sample= gtk.TreeViewColumn("Links")
treeview.append_column(column_text)
treeview.append_column(column_sample)
cellrenderer_text = gtk.CellRendererText()
column_text.pack_start(cellrenderer_text,False)
column_text.add_attribute(cellrenderer_text,"text",0)
cellrenderer_sample = gtk.CellRendererText()
column_sample.pack_start(cellrenderer_sample, False)
column_sample.add_attribute(cellrenderer_sample,"text", 0)
window.connect("destroy", lambda q : gtk.main_quit())
window.add(treeview)
window.show_all()
CellRenderer()
gtk.main()
我得到的结果窗口是:
您可能已经注意到,liststore
附加的列表在第二个索引中有链接。为什么第二列中没有显示任何链接?
答案 0 :(得分:1)
您指的是
处的相同索引column_sample.add_attribute(cellrenderer_sample,"text", 0)
将其更改为
column_sample.add_attribute(cellrenderer_sample,"text", 1)
完整档案。
import gtk
class CellRenderer:
def __init__(self):
window = gtk.Window()
window.set_default_size(200,200)
liststore = gtk.ListStore(str, str)
liststore.append(["Fedora","http://fedoraproject.org"])
liststore.append(["Slackware","http://www.slackware.com"])
liststore.append(["Sidux","http://sidux.com"])
treeview = gtk.TreeView(liststore)
column_text = gtk.TreeViewColumn("Distributions")
column_sample= gtk.TreeViewColumn("Links")
treeview.append_column(column_text)
treeview.append_column(column_sample)
cellrenderer_text = gtk.CellRendererText()
column_text.pack_start(cellrenderer_text,False)
column_text.add_attribute(cellrenderer_text,"text",0)
cellrenderer_sample = gtk.CellRendererText()
column_sample.pack_start(cellrenderer_sample, False)
column_sample.add_attribute(cellrenderer_sample,"text", 1)
window.connect("destroy", lambda q : gtk.main_quit())
window.add(treeview)
window.show_all()
CellRenderer()
gtk.main()