Pygtk Liststore 最近调用在 Liststore 更新之后
Pygtk Liststore most recent call last after Liststore update
我在 PyGtk 中创建了显示名称列表的简单代码。单击“更新”按钮后,将更新名称列表。不幸的是,我无法摆脱单击“更新”按钮后出现的“最近一次通话”错误。谁能就这个问题给我建议?这大概是“#select and row”下的一行代码的问题。
编辑:
脚本 returns 错误信息:
Traceback (most recent call last):
File "/home/radek/Desktop/stackowr1.py", line 18, in choiceRow
line = model[treeiter][0]
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 849, in __getitem__
aiter = self._getiter(key)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 837, in _getiter
aiter = self.get_iter(key)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 871, in get_iter
path = self._coerce_path(path)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 846, in _coerce_path
return TreePath(path)
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1210, in __new__
path = ":".join(str(val) for val in path)
TypeError: 'NoneType' object is not iterable
我的代码:
```python
# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.set_border_width(3)
self.set_default_size(800, 600)
self.name1 = "John"
self.name2 = "George"
def choiceRow(selection):
model, treeiter = selection.get_selected()
line = model[treeiter][0]
print(line)
def update_name(self):
self.name1 = "Jeane"
self.name2 = "Margot"
print(self.name1, self.name2)
win.liststore.clear()
win.liststore.append([self.name1])
win.liststore.append([self.name2])
button = Gtk.Button(label = "Update")
button.connect("clicked", update_name)
self.layout = Gtk.Layout()
self.tree = Gtk.TreeView()
self.liststore = Gtk.ListStore(str)
self.tree.set_model(self.liststore)
self.liststore.append([self.name1])
self.liststore.append([self.name2])
render = Gtk.CellRendererText()
self.column = Gtk.TreeViewColumn("ID", render, text=0)
self.tree.append_column(self.column)
# select a row
selectetRow = self.tree.get_selection()
selectetRow.connect("changed", choiceRow)
self.layout.put(self.tree, 0,0)
self.layout.put(button, 0,100)
self.add(self.layout)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Gtk.TreeSelection.get_selected returns None
当没有选择任何东西时,在更新调用之后没有选择任何东西所以你得到 None
for treeiter
然后你尝试访问 model[None][0]
显然一定会失败。您需要在尝试使用之前检查返回的 iter
是否有效,因此只需将 choiceRow
函数更改为
def choiceRow(selection):
model, treeiter = selection.get_selected()
if treeiter:
line = model[treeiter][0]
print(line)
我在 PyGtk 中创建了显示名称列表的简单代码。单击“更新”按钮后,将更新名称列表。不幸的是,我无法摆脱单击“更新”按钮后出现的“最近一次通话”错误。谁能就这个问题给我建议?这大概是“#select and row”下的一行代码的问题。
编辑: 脚本 returns 错误信息:
Traceback (most recent call last): File "/home/radek/Desktop/stackowr1.py", line 18, in choiceRow line = model[treeiter][0] File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 849, in __getitem__ aiter = self._getiter(key) File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 837, in _getiter aiter = self.get_iter(key) File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 871, in get_iter path = self._coerce_path(path) File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 846, in _coerce_path return TreePath(path) File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1210, in __new__ path = ":".join(str(val) for val in path) TypeError: 'NoneType' object is not iterable
我的代码:
```python
# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.set_border_width(3)
self.set_default_size(800, 600)
self.name1 = "John"
self.name2 = "George"
def choiceRow(selection):
model, treeiter = selection.get_selected()
line = model[treeiter][0]
print(line)
def update_name(self):
self.name1 = "Jeane"
self.name2 = "Margot"
print(self.name1, self.name2)
win.liststore.clear()
win.liststore.append([self.name1])
win.liststore.append([self.name2])
button = Gtk.Button(label = "Update")
button.connect("clicked", update_name)
self.layout = Gtk.Layout()
self.tree = Gtk.TreeView()
self.liststore = Gtk.ListStore(str)
self.tree.set_model(self.liststore)
self.liststore.append([self.name1])
self.liststore.append([self.name2])
render = Gtk.CellRendererText()
self.column = Gtk.TreeViewColumn("ID", render, text=0)
self.tree.append_column(self.column)
# select a row
selectetRow = self.tree.get_selection()
selectetRow.connect("changed", choiceRow)
self.layout.put(self.tree, 0,0)
self.layout.put(button, 0,100)
self.add(self.layout)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Gtk.TreeSelection.get_selected returns None
当没有选择任何东西时,在更新调用之后没有选择任何东西所以你得到 None
for treeiter
然后你尝试访问 model[None][0]
显然一定会失败。您需要在尝试使用之前检查返回的 iter
是否有效,因此只需将 choiceRow
函数更改为
def choiceRow(selection):
model, treeiter = selection.get_selected()
if treeiter:
line = model[treeiter][0]
print(line)