[Sugar-devel] How to make a GTK3 EventBox accept the focus?

James Simmons nicestep at gmail.com
Fri Mar 1 20:18:41 EST 2013


Daniel,

Your code was very helpful.  As it turned out, my event box code was
working fine.  When Aneesh Dogra updated the code to use GTK3 he neglected
to change the use of .value in Gtk.Adjustment to get_value() and
set_value().  As a result the Activity was processing the arrow keys OK but
could not page through the images because the code to adjust the image in
the scroller was broken.  That's an easy mistake to make and it took me a
long time to find, and your code samples helped me figure out what wasn't
causing the problem.

Thanks again,

James Simmons

On Fri, Mar 1, 2013 at 1:12 PM, Daniel Narvaez <dwnarvaez at gmail.com> wrote:

> Sigh, I hoped gtk3 had got rid of the no-window widgets mess...
>
> I think you need to use EventBox with a window and grab_focus on
> button_press events. See the attached test.
>
> On 1 March 2013 18:32, James Simmons <nicestep at gmail.com> wrote:
> > Daniel,
> >
> > I played with your code last night at home.  I modified it so it actually
> > loads pictures in the left and right Gtk.Image.  I found that when I
> tabbed
> > through the controls in the window that the focus_in and focus_out
> methods
> > were invoked.  However, when I tried to change the focus by clicking on
> > either image nothing happened.  Before trying this I changed View Slides
> to
> > eliminate the event box and the Activity still doesn't work.
> >
> > I added code like this:
> >
> > self.image1.set_events(Gdk.EventMask.KEY_PRESS_MASK |
> > Gdk.EventMask.BUTTON_PRESS_MASK)
> >
> > to your example but it had no effect.  You can tab into an Image but you
> > can't make it take the focus by clicking on it.
> >
> > This link suggests that the event box is necessary:
> >
> >
> http://stackoverflow.com/questions/14861503/catching-button-click-on-gtk-image-python-gtk3
> >
> > I'm going to fool with this some more this weekend.
> >
> > James Simmons
> >
> > On Thu, Feb 28, 2013 at 10:20 AM, Daniel Narvaez <dwnarvaez at gmail.com>
> > wrote:
> >>
> >> GtkImage only works for me:
> >>
> >> from gi.repository import Gtk
> >>
> >> def image1_focus_in_cb(a, b):
> >>     print("focus in 1")
> >>
> >> def image1_focus_out_cb(a, b):
> >>     print("focus out 1")
> >>
> >> def image2_focus_in_cb(a, b):
> >>     print("focus in 2")
> >>
> >> def image2_focus_out_cb(a, b):
> >>     print("focus out 2")
> >>
> >>
> >> window = Gtk.Window()
> >> box = Gtk.HBox()
> >>
> >> image1 = Gtk.Image.new_from_file("")
> >> image1.set_can_focus(True)
> >>
> >> image1.connect("focus-in-event", image1_focus_in_cb)
> >> image1.connect("focus-out-event", image1_focus_out_cb)
> >>
> >> image2 = Gtk.Image.new_from_file("")
> >> image2.set_can_focus(True)
> >>
> >> image2.connect("focus-in-event", image2_focus_in_cb)
> >> image2.connect("focus-out-event", image2_focus_out_cb)
> >>
> >> box.pack_start(image1, False, False, 0)
> >> box.pack_start(image2, False, False, 0)
> >>
> >> window.add(box)
> >> window.show_all()
> >>
> >> Gtk.main()
> >>
> >>
> >> On 28 February 2013 17:06, Daniel Narvaez <dwnarvaez at gmail.com> wrote:
> >> > Two things you could try:
> >> >
> >> > - set_visible_window(True) the event box and also set_can_focus
> >> >
> >> > - Remove the box and set_can_focus the image.
> >> >
> >> >
> >> > On Thursday, 28 February 2013, James Simmons wrote:
> >> >>
> >> >> Gonzalo,
> >> >>
> >> >> The problem with that it I want the arrow keys to page through the
> >> >> images
> >> >> only when the image has the focus.  There are other places in the
> >> >> Activity
> >> >> where I don't want to interfere with the arrow keys.  For instance,
> you
> >> >> can
> >> >> make annotations for a specific image.
> >> >>
> >> >> I notice that GtkImage is a subclass of GtkWidget, so maybe that
> means
> >> >> that I don't need the event box anymore.  Maybe the image itself can
> >> >> accept
> >> >> the focus and receive the events.  The problem I have with that is
> that
> >> >> the
> >> >> old GTK made Image extend Widget also, and as I remember it you
> >> >> couldn't get
> >> >> events from an Image.  It was years ago.  Maybe I've been doing it
> >> >> wrong the
> >> >> whole time.
> >> >>
> >> >> James Simmons
> >> >>
> >> >>
> >> >> On Wed, Feb 27, 2013 at 6:24 PM, Gonzalo Odiard <gonzalo at laptop.org>
> >> >> wrote:
> >> >>>
> >> >>> Can you catch the key-press-event in the activity class?
> >> >>> I think other activities like Read or Terminal do this.
> >> >>>
> >> >>> Gonzalo
> >> >>>
> >> >>> On Wed, Feb 27, 2013 at 1:53 PM, James Simmons <nicestep at gmail.com>
> >> >>> wrote:
> >> >>>>
> >> >>>> Aneesh,
> >> >>>>
> >> >>>> I'm trying to incorporate your GTK3 update to View Slides and
> release
> >> >>>> the result on ASLO.  The problem I'm still having is that I cannot
> >> >>>> page
> >> >>>> through the images using the keyboard as I used to.  What I did in
> >> >>>> the past
> >> >>>> was to nest the image control in an event box and make the event
> box
> >> >>>> able to
> >> >>>> accept the focus.  Then the user clicks on the image with the mouse
> >> >>>> to set
> >> >>>> the focus and at that point the event box receives key press
> events.
> >> >>>>
> >> >>>> When you upgraded View Slides to use GTK3 you removed the code to
> >> >>>> make
> >> >>>> the event box focusable.  I have tried to replace the missing line
> of
> >> >>>> code
> >> >>>> with what seems to be the GTK3 equivalent but it isn't working.
>  I've
> >> >>>> done a
> >> >>>> lot of google searches trying to figure out the problem and I'm not
> >> >>>> getting
> >> >>>> anywhere.  I cannot release View Slides as it is.  It really needs
> to
> >> >>>> be
> >> >>>> able to navigate through the images with the keyboard to be usable.
> >> >>>>
> >> >>>> I'm hoping you might have some thoughts or maybe an idea of how to
> do
> >> >>>> the keyboard paging a different way.
> >> >>>>
> >> >>>> James Simmons
> >> >>>>
> >> >>>>
> >> >>>> On Mon, Feb 25, 2013 at 6:20 AM, Manuel Quiñones <manuq at laptop.org
> >
> >> >>>> wrote:
> >> >>>>>
> >> >>>>> Hi James,
> >> >>>>>
> >> >>>>> 2013/2/24 James Simmons <nicestep at gmail.com>:
> >> >>>>> > I was looking at the upgrade to GTK3 done for View Slides and it
> >> >>>>> > seems that
> >> >>>>> > it no longer responds to the keyboard.  What I had done
> previously
> >> >>>>> > to
> >> >>>>> > get it
> >> >>>>> > to do that was to put the image I was displaying in an event
> box.
> >> >>>>> > I
> >> >>>>> > then
> >> >>>>> > made the event box accept the focus like this:
> >> >>>>> >
> >> >>>>> > self.eventbox.set_events(gtk.gdk.KEY_PRESS_MASK |
> >> >>>>> > gtk.gdk.BUTTON_PRESS_MASK)
> >> >>>>> > self.eventbox.set_flags(gtk.CAN_FOCUS)
> >> >>>>> >
> >> >>>>> > The Gtk3 port changed the code to this:
> >> >>>>> >
> >> >>>>> > self.eventbox.set_events(Gdk.EventMask.KEY_PRESS_MASK |
> >> >>>>> > Gdk.EventMask.BUTTON_PRESS_MASK)
> >> >>>>> >
> >> >>>>> > There was no code to make it accept the focus.  I tried adding
> >> >>>>> > this:
> >> >>>>> >
> >> >>>>> > self.eventbox.set_can_focus(True)
> >> >>>>> >
> >> >>>>> > This does not prevent the Activity from running, but it doesn't
> >> >>>>> > make
> >> >>>>> > the
> >> >>>>> > Event Box focusable either.
> >> >>>>>
> >> >>>>> Have you tried self.eventbox.grab_focus() ?
> >> >>>>>
> >> >>>>>
> >> >>>>>
> >> >>>>>
> http://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-grab-focus
> >> >>>>>
> >> >>>>> --
> >> >>>>> .. manuq ..
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>> _______________________________________________
> >> >>>> Sugar-devel mailing list
> >> >>>> Sugar-devel at lists.sugarlabs.org
> >> >>>> http://lists.sugarlabs.org/listinfo/sugar-devel
> >> >>>>
> >> >>>
> >> >>
> >> >
> >> >
> >> > --
> >> > Daniel Narvaez
> >> >
> >>
> >>
> >>
> >> --
> >> Daniel Narvaez
> >
> >
>
>
>
> --
> Daniel Narvaez
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sugarlabs.org/archive/sugar-devel/attachments/20130301/af149baf/attachment.html>


More information about the Sugar-devel mailing list