On 2003-06-07, Grzegorz Adam Hankiewicz <[EMAIL PROTECTED]> wrote:
> I'm capturing the keyboard events of a TreeView to delete rows with
> keys. This works fine, and I also manage to select the next row after
> deletion or the previous one if the deleted row was the last one.
>
> However, the keyboard focus (a dotted rectangle) seems to be lost,
> so while I've selected the fifth row, pressing down once will select
> the second row. How can I solve this incoherency between selected
> row and widget keyboard focus?
This example illustrates the problem. Select with your cursor keys
one of the entries in the middle of the list and press your delete
key. The item will be deleted and the next one will be automatically
selected. But if you press your down key the second element of the
list will be selected, ignoring the previous selection.
--
Please don't send me private copies of your public answers. Thanks.
# -*- mode:Python; tab-width: 3 -*-
print __name__
import gtk
import gobject
class Resources(gtk.Window):
def __init__(self):
super(Resources, self).__init__(gtk.WINDOW_TOPLEVEL)
self.counter = 1
self.connect("delete_event", lambda widget, event: 0)
self.connect("destroy", lambda x: gtk.main_quit())
scrolled_window = gtk.ScrolledWindow()
self.add(scrolled_window)
scrolled_window.set_shadow_type(gtk.SHADOW_ETCHED_IN)
scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled_window.set_size_request(400, 250)
self.data = ["One", "Two", "Three", "Four", "Five"] * 5
model = self.update_model(gtk.ListStore(gobject.TYPE_STRING))
self.list_view = list_view = gtk.TreeView(model)
scrolled_window.add(list_view)
list_view.set_rules_hint(1)
list_view.add_events(gtk.gdk.KEY_PRESS)
list_view.connect("key_press_event", self.keyboard_event)
list_view.add_events(gtk.gdk.BUTTON_PRESS)
list_view.append_column(gtk.TreeViewColumn('blah',
gtk.CellRendererText(), text = 0))
self.show_all()
def keyboard_event(self, widget, event):
if gtk.gdk.keyval_name(event.keyval) == "Delete":
self.delete_resource()
elif gtk.gdk.keyval_name(event.keyval) == "Insert":
self.new_resource()
def update_model(self, model):
model.clear()
self.data.sort()
for item in self.data: model.set(model.append(), 0, item)
return model
def new_resource(self, *args):
while 1:
self.counter += 1
id = "item%02d" % self.counter
if not id in self.data: break
self.data.append(id)
self.update_model(self.list_view.get_model())
# this function should somehow set correctly the keyboard focus
def delete_resource(self, *args):
selection = self.list_view.get_selection()
model, iter, = selection.get_selected()
if iter:
path = model.get_path(iter)
del self.data[path[0]]
model.remove(iter)
selection.select_path(path)
if not selection.path_is_selected(path):
row = path[0]-1
if row >= 0:
selection.select_path((row,))
if __name__ == "__main__":
Resources()
gtk.main()
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/