Connecting Tech Pros Worldwide Forums | Help | Site Map

wxGrid and Focus Event

lux
Guest
 
Posts: n/a
#1: Nov 29 '05
Hi,
How can I capture the EVT_SET_FOCUS on a wxGrid?

Tank's in advance
Luca


Paul McNett
Guest
 
Posts: n/a
#2: Nov 29 '05

re: wxGrid and Focus Event


lux wrote:[color=blue]
> How can I capture the EVT_SET_FOCUS on a wxGrid?[/color]

If you want to catch when the grid as a whole gets the focus, use:
[color=blue][color=green][color=darkred]
>>> grid.Bind(wx.grid.EVT_SET_FOCUS, self.onGridFocus)[/color][/color][/color]


If you want to catch when a cell in the grid gets the focus, use:
[color=blue][color=green][color=darkred]
>>> grid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.onCellSelected)[/color][/color][/color]


And, your questions will be better on the wxpython-users list at
http://wxpython.org/maillist.php


--
Paul McNett
http://paulmcnett.com
http://dabodev.com

Paul McNett
Guest
 
Posts: n/a
#3: Nov 29 '05

re: wxGrid and Focus Event


Paul McNett wrote:[color=blue]
> lux wrote:
>[color=green]
>>How can I capture the EVT_SET_FOCUS on a wxGrid?[/color]
>
>
> If you want to catch when the grid as a whole gets the focus, use:
>[color=green][color=darkred]
> >>> grid.Bind(wx.grid.EVT_SET_FOCUS, self.onGridFocus)[/color][/color][/color]

Oops, my bad:
[color=blue][color=green][color=darkred]
>>> grid.Bind(wx.EVT_SET_FOCUS, self.onGridFocus)[/color][/color][/color]

[color=blue]
> If you want to catch when a cell in the grid gets the focus, use:
>[color=green][color=darkred]
> >>> grid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.onCellSelected)[/color][/color]
>
>
> And, your questions will be better on the wxpython-users list at
> http://wxpython.org/maillist.php
>
>[/color]


--
Paul McNett
http://paulmcnett.com
http://dabodev.com

lux
Guest
 
Posts: n/a
#4: Nov 29 '05

re: wxGrid and Focus Event


Can you try this code?

If you press only the TAB key
the focus go from the TextCtrl to the Grid (I suppose)
but onGridFocus in not called.

any idea?
Luca.


##################

import wx
import wx.grid

app = wx.PySimpleApp()

f = wx.Frame(None, -1, "")
p = wx.Panel(f, -1)
s = wx.BoxSizer(wx.VERTICAL)

t1 = wx.TextCtrl(p)
s.Add(t1)

g = wx.grid.Grid(p, -1)
g.CreateGrid(2, 2)
s.Add(g, 1, wx.EXPAND)

def onGridFocus(evt):
print "onGridFocus"
evt.Skip()

def onCellSelected(evt):
print "onCellSelected"
evt.Skip()

g.Bind(wx.grid.EVT_GRID_SELECT_CELL, onCellSelected)
g.Bind(wx.EVT_SET_FOCUS, onGridFocus)

p.SetSizer(s)
f.Show()

app.MainLoop()

##################

Paul McNett
Guest
 
Posts: n/a
#5: Nov 29 '05

re: wxGrid and Focus Event


lux wrote:[color=blue]
> Can you try this code?[/color]

Sure, thanks for posting it!

[color=blue]
> If you press only the TAB key
> the focus go from the TextCtrl to the Grid (I suppose)
> but onGridFocus in not called.[/color]

Confirmed, at least on Gtk.

[color=blue]
> any idea?[/color]

Yep, the grid is actually a collection of subwindows, and I made the guess that
the first window to get the focus is actually that little corner window at the
intersection of the column labels and the row labels. Try this instead:
[color=blue][color=green][color=darkred]
>>> g.GetGridCornerLabelWindow().Bind(wx.EVT_SET_FOCUS , onGridFocus)[/color][/color][/color]


--
Paul McNett
http://paulmcnett.com
http://dabodev.com

lux
Guest
 
Posts: n/a
#6: Nov 29 '05

re: wxGrid and Focus Event


TANKS!!!
Now it work!!!

Luca

Paul McNett
Guest
 
Posts: n/a
#7: Nov 29 '05

re: wxGrid and Focus Event


lux wrote:[color=blue]
> TANKS!!!
> Now it work!!![/color]

Not so fast. I've found out that I had to do the following ugly workaround to
ensure it works in all cases:

def _initEvents(self):
...
if self.BaseClass.__name__ == "dGrid":
## Ugly workaround for grids not firing focus events from the keyboard
## correctly.
self._lastGridFocusTimestamp = 0.0
self.GetGridCornerLabelWindow().Bind(wx.EVT_SET_FO CUS, self.__onWxGotFocus)
self.GetGridColLabelWindow().Bind(wx.EVT_SET_FOCUS , self.__onWxGotFocus)
self.GetGridRowLabelWindow().Bind(wx.EVT_SET_FOCUS , self.__onWxGotFocus)
self.GetGridWindow().Bind(wx.EVT_SET_FOCUS, self.__onWxGotFocus)
self.Bind(wx.EVT_SET_FOCUS, self.__onWxGotFocus)
...


def __onWxGotFocus(self, evt):
if self.BaseClass.__name__ == "dGrid":
## Continuation of ugly workaround for grid focus event. Only raise the
## Dabo event if we are reasonably sure it isn't a repeat.
prev = self._lastGridFocusTimestamp
now = self._lastGridFocusTimestamp = time.time()
if now-prev < .05:
return
self.raiseEvent(dEvents.GotFocus, evt)

--
Paul McNett
http://paulmcnett.com
http://dabodev.com

Bugs
Guest
 
Posts: n/a
#8: Nov 30 '05

re: wxGrid and Focus Event


So Paul, are you saying there's a bug with the wxGrid control and if so,
do you know if there's been a bug-report submitted to the wxWidgets
and/or wxPython folks?
Or is this just the way the wxGrid control works?
Thanks!


Paul McNett wrote:[color=blue]
> Not so fast. I've found out that I had to do the following ugly
> workaround to ensure it works in all cases:
>[/color]
Paul McNett
Guest
 
Posts: n/a
#9: Nov 30 '05

re: wxGrid and Focus Event


Bugs wrote:[color=blue]
> So Paul, are you saying there's a bug with the wxGrid control and if so,[/color]

Yes, I think it is a bug.

[color=blue]
> do you know if there's been a bug-report submitted to the wxWidgets
> and/or wxPython folks?[/color]

I don't know, but I've been meaning to check.

[color=blue]
> Or is this just the way the wxGrid control works?
> Thanks![/color]

wxPython/wxWidgets, like any GUI toolkit, is pretty complex. For the most part
all the commonly-needed things work just fine - it is when you venture into the
less-used things that you get into trouble.

If I filed a proper bug report for everything wrong with wxPython/wxWidgets, I'd
probably not get anything else done. But on the other hand you couldn't force me
to stop using wxPython if you tried!


--
Paul McNett
http://paulmcnett.com
http://dabodev.com

Bugs
Guest
 
Posts: n/a
#10: Nov 30 '05

re: wxGrid and Focus Event


Paul McNett wrote:[color=blue]
>
> If I filed a proper bug report for everything wrong with
> wxPython/wxWidgets, I'd probably not get anything else done. But on the
> other hand you couldn't force me to stop using wxPython if you tried!
>[/color]

Like any open-source software, the community is what makes it better.
I agree, it's still a great toolkit even with the bugs.
But think how much better a toolkit it would be with fewer bugs! =)

Thanks Paul
Magnus Lycka
Guest
 
Posts: n/a
#11: Dec 5 '05

re: wxGrid and Focus Event


Paul McNett wrote:[color=blue]
> Bugs wrote:
>[color=green]
>> So Paul, are you saying there's a bug with the wxGrid control and if so,[/color]
>
> Yes, I think it is a bug.[/color]

I'm not so sure. I seem to remember being told on the mailing list
that I had to check the specific sub-window for events. Even if it's
by design, it's certainly questionable whether this behaviour is
sane though. I guess it's a wxWidgets issue rather than a wxPython
issue though.
Closed Thread


Similar Python bytes