Re: How to set focus on a text view when tree selection changed?



Excellent! I am using gobject.idle_add instead of glib, though.

Thank you so much, David.

Kenny


=======================


#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk, gobject


def wrap(widget):
    sw = gtk.ScrolledWindow()
    sw.add(widget)
    sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
    sw.set_border_width(1)
    return sw


def on_selection_changed(selection, textview):
    def grab_this(textview):
        textview.grab_focus()
        return False
    gobject.idle_add(grab_this, textview)
    return False


if __name__ == "__main__":
    paned = gtk.HPaned()

    treestore = gtk.TreeStore(str)

    new_iter = treestore.append(None)
    treestore.set(new_iter, 0, '00000')
    new_iter = treestore.append(None)
    treestore.set(new_iter, 0, '11111')
    new_iter = treestore.append(None)
    treestore.set(new_iter, 0, '22222')
    new_iter = treestore.append(None)
    treestore.set(new_iter, 0, '33333')
    new_iter = treestore.append(None)
    treestore.set(new_iter, 0, '44444')

    treeview = gtk.TreeView(treestore)
    treeview.set_enable_search(False)

    column = gtk.TreeViewColumn();
    renderer = gtk.CellRendererText()
    column.pack_start(renderer, False)
    column.add_attribute(renderer, 'text', 0)
    treeview.append_column(column)

    paned.add1(wrap(treeview))

    textview = gtk.TextView()
    paned.add2(wrap(textview))
    paned.set_position(250)

    treeview.get_selection().connect_after('changed', on_selection_changed, textview)

    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_default_size(800, 600)
    window.add(paned)
    window.show_all()

    gtk.main()








2010/3/8 David Nečas <yeti physics muni cz>
On Mon, Mar 08, 2010 at 09:55:02AM -0800, kenny wang wrote:
> Hope for better solutions.

If you can stand (or, better, handle) a slight race condition:

import gtk, gobject, glib, glob

def grab_focus(self, widget):
   def grab_this(widget):
       widget.grab_focus()
       return False
   glib.idle_add(grab_this, widget)

window = gtk.Window()
window.set_default_size(0, 400)

hbox = gtk.HBox(False, 2)
window.add(hbox)

scwin = gtk.ScrolledWindow()
scwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
hbox.pack_start(scwin)

treemodel = gtk.ListStore(gobject.TYPE_STRING)
for x in glob.glob('*'):
   treemodel.append((x, ))

tree = gtk.TreeView(treemodel)
scwin.add(tree)

column = gtk.TreeViewColumn('Name', gtk.CellRendererText(), text=0)
tree.append_column(column)

textmodel = gtk.TextBuffer()
textmodel.set_text('Some text goes here.')

text = gtk.TextView(textmodel)
hbox.pack_start(text)

window.connect('destroy', gtk.main_quit)
selection = tree.get_selection()
selection.connect_after('changed', grab_focus, text)

window.show_all()
gtk.main()


Yeti




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]