wxGrid and Focus Event

L

lux

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()

##################
 
P

Paul McNett

lux said:
Can you try this code?

Sure, thanks for posting it!

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

Confirmed, at least on Gtk.

any idea?

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:
 
P

Paul McNett

lux said:
TANKS!!!
Now it work!!!

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_FOCUS, 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)
 
B

Bugs

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!
 
P

Paul McNett

Bugs said:
So Paul, are you saying there's a bug with the wxGrid control and if so,

Yes, I think it is a bug.

do you know if there's been a bug-report submitted to the wxWidgets
and/or wxPython folks?

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

Or is this just the way the wxGrid control works?
Thanks!

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!
 
B

Bugs

Paul said:
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!

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
 
M

Magnus Lycka

Paul said:
Yes, I think it is a bug.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top