python - 无法以编程方式将 TreeView 添加到 Box
问题描述
我试图在单击按钮时以编程方式添加 TreeView,但没有运气。这是我的例子:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, Gdk
class TreeView(Gtk.TreeView):
def __init__(self, items):
Gtk.TreeView.__init__(self)
# Creating the ListStore model
model = Gtk.ListStore(int, str, str)
for item in items:
model.append(item)
# attach model and renderers
self.set_model(model)
for i, column_name in enumerate(['c1', 'c2', 'c3']):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_name, renderer, text = i)
self.append_column(column)
if __name__ == "__main__":
tv = [
[ 3, 'AST', 'Practica 1' ],
[ 5, 'AST', 'Practica 2' ],
[ 8, 'PBE', 'Practica 3' ]
]
def add(button):
table = TreeView(tv)
box.add(table)
win.show_all()
win = Gtk.Window()
win.set_position(Gtk.WindowPosition.CENTER)
win.set_border_width(10)
win.set_default_size(600, 400)
win.connect("destroy", Gtk.main_quit)
box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
button = Gtk.Button.new_with_label("Add")
button.connect("clicked", add)
box.add(button)
win.add(box)
win.show_all()
Gtk.main()
当我单击按钮时,只出现列名,没有数据。如果我写table = TreeView(tv)
并且box.add(table)
主要一切都很好。
在 ubuntu 16.04 上运行。gtk3 版本:3.18.9
编辑:
TreeView
交换两个 s 的示例。行为不一致,但在ListBox
仿真中效果很好:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, Gdk
def replace_widget(old, new):
parent = old.get_parent()
props = {}
for key in Gtk.ContainerClass.list_child_properties(type(parent)):
props[key.name] = parent.child_get_property(old, key.name)
parent.remove(old)
parent.pack_start(new, False, False, 0)
for name, value in props.items():
parent.child_set_property(new, name, value)
new.show_all()
# native TreeView: doesn't work!
class TreeView(Gtk.TreeView):
def __init__(self, items, **kwargs):
# Creating the ListStore model
model = Gtk.ListStore(*[type(value) for value in items[0].values()])
for item in items:
model.append(list(item.values()))
# Creating the TreeView
super().__init__(model = model, **kwargs)
#self.set_model(model)
self.get_selection().set_mode(Gtk.SelectionMode.NONE) # disable selection
for i, column_name in enumerate(list(items[0].keys())):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_name, renderer, text = i)
column.props.expand = True
self.append_column(column)
'''
# TreeView ListBox emulation: works!
class TreeView(Gtk.ListBox):
def __init__(self, items, **kwargs):
super().__init__(**kwargs)
# header
row = Gtk.ListBoxRow()
self.add(row)
box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, homogeneous = True)
row.add(box)
for header in items[0].keys():
header_label = Gtk.Label(header, halign = Gtk.Align.START)
box.pack_start(header_label, True, True, 0)
# data rows
for i, item in enumerate(items):
row = Gtk.ListBoxRow()
self.add(row)
box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, homogeneous = True)
row.add(box)
for column in item.values():
box.pack_start(Gtk.Label(str(column), halign = Gtk.Align.START), True, True, 0)
'''
if __name__ == "__main__":
tasks = [
{ 'date': '2018-09-30', 'subject': 'AST', 'name': 'Pràctica 1' },
{ 'date': '2018-10-10', 'subject': 'AST', 'name': 'Pràctica 2' },
{ 'date': '2018-12-05', 'subject': 'AST', 'name': 'Pràctica 3' },
{ 'date': '2018-12-08', 'subject': 'AST', 'name': 'Pràctica 4' },
{ 'date': '2018-10-08', 'subject': 'PBE', 'name': 'Project Plan' },
{ 'date': '2018-10-22', 'subject': 'PBE', 'name': 'Requirement Specifications' },
{ 'date': '2018-11-25', 'subject': 'PBE', 'name': 'Critical Design Report' },
{ 'date': '2018-12-13', 'subject': 'PBE', 'name': 'Final Report' }
]
marks = [
{ 'subject': 'AST', 'name': 'control teoria', 'mark': 7.5, 'student': 'C5CC358A' },
{ 'subject': 'AST', 'name': 'Lab 1', 'mark': 3.5, 'student': 'C5CC358A' },
{ 'subject': 'AST', 'name': 'Lab 2', 'mark': 4.7, 'student': 'C5CC358A' },
{ 'subject': 'AST', 'name': 'final', 'mark': 8.6, 'student': 'C5CC358A' },
{ 'subject': 'PBE', 'name': 'puzzle1', 'mark': 2.8, 'student': 'C5CC358A' },
{ 'subject': 'PBE', 'name': 'puzzle2', 'mark': 8.4, 'student': 'C5CC358A' },
{ 'subject': 'PBE', 'name': 'control', 'mark': 7.3, 'student': 'C5CC358A' },
{ 'subject': 'PBE', 'name': 'CDR', 'mark': 8, 'student': 'C5CC358A' },
{ 'subject': 'PBE', 'name': 'FR', 'mark': 9, 'student': 'C5CC358A' }
]
def clicked(button):
replace_widget(first, TreeView(marks))
win = Gtk.Window()
win.set_position(Gtk.WindowPosition.CENTER)
win.set_border_width(10)
win.set_default_size(600, 400)
win.connect("destroy", Gtk.main_quit)
box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
button = Gtk.Button.new_with_label("Replace")
button.connect("clicked", clicked)
first = TreeView(tasks)
box.pack_start(first, False, False, 0)
box.pack_start(button, False, False, 0)
win.add(box)
win.show_all()
Gtk.main()
解决方案
推荐阅读
- python - 如何将sql查询输出从元组列表转换为python3中的字典列表,其中每个字典代表表中的一行?
- azure - 天蓝色 | 自动进稿器 | 在 sql 表中记录管道运行(执行详细信息)
- c++ - 最小移动到相等的数组元素有助于理解解决方案
- python - Python 在不同的编辑器或 IDE 中是否以不同的方式编写?
- wordpress - 从 wordpress 中删除了所有插件,但仍然显示广告
- c# - itext7 HTML to PDF 忽略列宽
- date - 如何使用微秒(2020-10-06T08:52:54.3556219Z)和使用 Java 8 获得 now() 和 TimeStamp 之间的秒差?
- r - 将包含时间的列转换为日期
- android-activity - 当手机被锁定并且屏幕在 android 10 中关闭时,如何从 ForegroundService 打开活动?
- c# - 在 .NET Core 中使用 CMS 和 RSACng 进行签名