提问人:fauve 提问时间:4/17/2020 最后编辑:fauve 更新时间:5/19/2021 访问量:708
如何将“隐藏数据”分配给Gtk.TreeView行,以便使用Gtk.TreeView.connect(“row-activated”)捕获它们?
How to assign “hidden data” to Gtk.TreeView row in order to catch them with Gtk.TreeView.connect("row-activated")?
问:
在 Python 中,我有一个类 witch contain 和 and 属性(实际上还有更多其他数据,但我在示例中对其进行了简化)。person
firstName
lastName
country
因此,我生成了一个表女巫应该只显示和(用户不应该看到国家)。但是当用户单击特定行时,我想获取完整的对象。firstName
lastName
person
这是我的代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# MWE for hidden data in Gtk.TreeView
import sys
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import Pango
class person():
def __init__(self, firstName, lastName, country):
self.firstName=firstName
self.lastName=lastName
self.country=country
personsList=[
person("Ada", "Lovelace", "UK"),
person("Alan", "Turing", "UK"),
person("Denis", "Richie", "USA")
]
def onRowChange(widget, row, col):
# This function is executed when a user click on a row
print(widget)
print(row)
print(col)
pass
class GridWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeView MWE")
self.connect("destroy", Gtk.main_quit)
# Setting up the self.grid in which the elements are to be positionned
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
# Preparing the list store
listStore=Gtk.ListStore(str, str)
for aPerson in personsList:
listStore.append([aPerson.firstName, aPerson.lastName])
# Seting up the TreeView and given the ListStore to the Treeview
treeView=Gtk.TreeView(model=listStore)
renderer_text = Gtk.CellRendererText()
# Preparing the headers
column_text = Gtk.TreeViewColumn("First Name", renderer_text, text=0)
treeView.append_column(column_text)
column_text = Gtk.TreeViewColumn("Last Name", renderer_text, text=1)
treeView.append_column(column_text)
# Setting the event connection
treeView.connect("row-activated", onRowChange)
# Attaching the treeView to the Gride
self.grid.add(treeView)
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
win = GridWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
这是渲染图:
正如你所看到的,根据row_activated
信号的文档,该函数只有在用户点击它时才能获得 和 对象。onRowChange()
TreeView
TreeViewColumn
我知道我可以得到行号并在列表中搜索女巫对象有这个行号,但它太危险了,而且有点笨拙。
那么,它们是否是一种将用户隐藏数据包含在用户中并用信号捕获这些数据的方法?Gtk.ListStore
row-activated
答:
0赞
elya5
4/17/2020
#1
您可以只向列表库添加数据,而无需在树视图中显示数据。 以下代码片段显示了必要的更改:
...
def onRowChange(widget, row, col):
# This function is executed when a user click on a row
model = widget.get_model()
iter_ = model.get_iter(row)
person = model.get_value(iter_, 2)
print(person.country)
class GridWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeView MWE")
self.connect("destroy", Gtk.main_quit)
# Setting up the self.grid in which the elements are to be positionned
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
# Preparing the list store
listStore=Gtk.ListStore(str, str, object)
for aPerson in personsList:
listStore.append([aPerson.firstName, aPerson.lastName, aPerson])
...
评论
0赞
fauve
4/18/2020
我应该期待这个答案。因此,该类实际上包含更多其他数据。我只是在示例中简化它。我真的需要羊毛数据,而不仅仅是一个属性。我只是修改问题来解释它。person
person
str
0赞
elya5
4/18/2020
我想我不理解这个问题。我将整个对象添加到该类中,以便您可以访问该类中的所有信息。这只是一个例子person
Gtk.ListStore
country
0赞
irs
4/5/2021
#2
请尝试以下示例。一些原始代码被注释掉,下面是用 #*** 突出显示的替换代码。
单击一行时,控制台应显示一个包含三个字段(名字、姓氏和国家/地区)的列表。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# MWE for hidden data in Gtk.TreeView
import sys
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import Pango
class person():
def __init__(self, firstName, lastName, country):
self.firstName=firstName
self.lastName=lastName
self.country=country
personsList=[
person("Ada", "Lovelace", "UK"),
person("Alan", "Turing", "UK"),
person("Denis", "Richie", "USA")
]
#*** Instead use: def on_row_activated(self, widget, row, col_view):
#def onRowChange(widget, row, col):
# # This function is executed when a user click on a row
# print(widget)
# print(row)
# print(col)
# pass
class GridWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TreeView MWE")
self.set_size_request(200,150)
self.connect("destroy", Gtk.main_quit)
# Setting up the self.grid in which the elements are to be positionned
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
# Preparing the list store
#listStore=Gtk.ListStore(str, str)
#*** Increase to three strings
listStore=Gtk.ListStore(str, str, str)
for aPerson in personsList:
#listStore.append([aPerson.firstName, aPerson.lastName])
#*** Add the country
listStore.append([aPerson.firstName, aPerson.lastName, aPerson.country])
# Seting up the TreeView and given the ListStore to the Treeview
treeView=Gtk.TreeView(model=listStore)
renderer_text = Gtk.CellRendererText()
# Preparing the headers
column_text = Gtk.TreeViewColumn("First Name", renderer_text, text=0)
treeView.append_column(column_text)
column_text = Gtk.TreeViewColumn("Last Name", renderer_text, text=1)
treeView.append_column(column_text)
# Setting the event connection
#treeView.connect("row-activated", onRowChange)
#*** Change connection so double-clicking on row calls self.on_row_activated
treeView.connect("row-activated", self.on_row_activated)
# Attaching the treeView to the Grid
self.grid.add(treeView)
#*** Change to using this callback when a row is double-clicked.
def on_row_activated(self, widget, row, col_view):
""" Calllback for double-click on Treeview row"""
model = widget.get_model()
row_list = []
for i in range(model.get_n_columns()):
#print(model[row][i])
row_list.append(model[row][i])
print(row_list)
# Example of what the console displays upon double-clicking a row...
# ['Alan', 'Turing', 'UK']
# ['Denis', 'Richie', 'USA']
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
win = GridWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
评论