Grid cell editing and button events (MSW)

David Hughes dfh at forestfield.co.uk
Thu Aug 10 08:35:05 PDT 2006


On a panel containing button(s) and a grid that has CellEditor(s), the =

buttons don't generate wx.EVT_BUTTON events if they are clicked while an =

editor is active.

A short, stand-alone example is attached that also contains a fuller =

description of the problem. As far as I can test, it is MSW only.

--
Regards,
David Hughes
-------------- next part --------------
# Module:       TestGrid.py
#
# Function:     Test grid - button problem
#
#               Behaviour on MSW at wx 2.6.1.0+  has changed. When in a gri=
d cell editor
#               clicking button closes and saves the edited value (internal=
 use
#               of kill-focus?) but EVT_BUTTON event does not appear - work=
 around by
#               catching button.EVT_LEFT_UP.
#               Conversely if the button is set as the default,
#               pressing ENTER while in a grid editor terminates the edit b=
ut also
#               triggers the button - work around by having no default butt=
on.
#
#               Also noticed that if the first button click is when a non-e=
dit
#               grid cell is selected, only get a mouse event - but get both
#               events on subsequent clicks.

# Copyright:    (c) 2006 Forestfield Software Ltd
#
# Created:      10 Aug 2006
#
#-----------------------------------------------------------------------
import os, sys, time

import wx
from wx.lib.mixins.grid import GridAutoEditMixin

class MyApp(wx.App):
    def OnInit(self):
        frame =3D TestFrame()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

#-----------------------------------------------------------------------

class TestFrame(wx.Frame):
    def __init__(self, size=3Dwx.DefaultSize, title=3D'', style=3Dwx.DEFAUL=
T_FRAME_STYLE):
        wx.Frame.__init__(self, parent=3DNone, size=3Dsize, title=3Dtitle, =
style=3Dstyle)
        self.Centre()
        p =3D TestPanel(self)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnCloseWindow(self, event):
        self.Destroy()

#-----------------------------------------------------------------------

class Agrid(wx.grid.Grid, GridAutoEditMixin):
    " General purpose grid class "
    def __init__(self, parent, id, pos=3Dwx.DefaultPosition, size=3Dwx.Defa=
ultSize, style=3Dwx.WANTS_CHARS):
        wx.grid.Grid.__init__(self, parent, id, pos, size, style|wx.SUNKEN_=
BORDER)
        GridAutoEditMixin.__init__(self)

#-----------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent):
        " Set up panel containing a grid and a button " =

        self.parent =3D parent
        wx.Panel.__init__(self, parent, -1)
        mainsizer =3D wx.BoxSizer(wx.HORIZONTAL)
        self.grid =3D Agrid(self, wx.NewId(), style=3Dwx.WANTS_CHARS)
        mainsizer.Add(self.grid, proportion=3D1, flag=3Dwx.EXPAND)         =
       =

        self.button =3D wx.Button(self, label=3D"Finish")
        self.button.SetDefault()
        mainsizer.Add(self.button, proportion=3D0, flag=3Dwx.ALIGN_CENTER|w=
x.ALL, border=3D20 )
        self.SetSizer(mainsizer)
        self.SetAutoLayout(True)
        =

        self.grid.CreateGrid( 4, 3)
        self.grid.SetDefaultCellAlignment(wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
        grid_data =3D [('row 1', 'abc'), ('row 2', 'def'), ('row 3', 'hij')=
, ('row 4', 'klm')] =

        for k in range(4):
            self.grid.SetCellValue(k, 0, grid_data[k][0])
            self.grid.SetReadOnly(k, 0, True)
            self.grid.SetCellValue(k, 1, grid_data[k][1])
            self.grid.SetCellEditor(k, 1, wx.grid.GridCellTextEditor())

        self.button.Bind(wx.EVT_BUTTON, self.OnButton)
        self.button.Bind(wx.EVT_LEFT_UP, self.OnMouse)

    def OnButton(self, event):
        self.grid.SaveEditControlValue()        # include any edit in-progr=
ess
        print '"Finish" caused by button ', repr(event.GetEventType()), rep=
r(event.IsCommandEvent())
        print
        =

        =

    def OnMouse(self, event):
        self.grid.SaveEditControlValue()        # include any edit in-progr=
ess
        print '"Finish" caused by mouse ', repr(event.GetEventType()), repr=
(event.IsCommandEvent())    =

        event.Skip()
        =


#-----------------------------------------------------------------------

if __name__ =3D=3D '__main__':
    app =3D MyApp(True)
    app.MainLoop()





More information about the wxpython-dev mailing list