Enter key in wx.grid

Hi,

I have a wx.grid table on a panel, I have set the grid so that it only accepts numbers. When I enter some number in a cell, and I press the ENTER button, the number is not saved in cell, nothing happens. I have to click on another cell in order to save that number to that cell. I don’t understand why this is happening. In wxpython demo, the simple grid example accepts ENTER key to save the entered value to cell. Why doesn’t my grid accept ENTER key? What can I do about it?

Best regards

Are you capturing key events? Can you provide a small runnable sample application that demonstrates the issue? It would also be good to know what OS, Python version and wxPython version you are using.

Mike

···

On Wednesday, April 2, 2014 7:48:23 AM UTC-5, steve wrote:

Hi,

I have a wx.grid table on a panel, I have set the grid so that it only accepts numbers. When I enter some number in a cell, and I press the ENTER button, the number is not saved in cell, nothing happens. I have to click on another cell in order to save that number to that cell. I don’t understand why this is happening. In wxpython demo, the simple grid example accepts ENTER key to save the entered value to cell. Why doesn’t my grid accept ENTER key? What can I do about it?

Best regards

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)

def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

       
    else:
        if (newKey != wx.WXK_DELETE) and (newKey != wx.WXK_BACK):
            #do not check if the user typed delete or backspace!
            #A Grid cell is made up of a TextCtrl.

            #Use TextCtrl methods to limit cell length.
            t = event.GetEventObject()
            x = t.GetValue()
            charCount = len(x)
            if charCount >= INPUT_LIMIT:

                #the max status field length has been exceeded
                data = x[0:INPUT_LIMIT]
                winsound.Beep(BEEP_FREQUENCY, BEEP_DURATION) #alert the user length exceeded
                t.SetValue("")

                t.AppendText(data)
            else:
                event.Skip()  #display the new key normally
        else:
            event.Skip()  #process the backspace or delete keys normally 
···

On Wed, Apr 2, 2014 at 8:48 AM, steve oslocourse@gmail.com wrote:

Hi,

I have a wx.grid table on a panel, I have set the grid so that it only accepts numbers. When I enter some number in a cell, and I press the ENTER button, the number is not saved in cell, nothing happens. I have to click on another cell in order to save that number to that cell. I don’t understand why this is happening. In wxpython demo, the simple grid example accepts ENTER key to save the entered value to cell. Why doesn’t my grid accept ENTER key? What can I do about it?

Best regards

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike
···

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)



def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)



def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike

Hi Mike,

Please check out the link I noted.

     https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

···

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

On Wednesday, April 2, 2014 11:40:59 AM UTC-4, Mike Driscoll wrote:

Hi Bruce,

I couldn’t get it work, here is what I do:

self.CreateGrid(12, 7)
self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.on_enter)

def on_enter(self, event):
print “editor starts”

    def HandleEnter(event):
         if event.GetKeyCode() == wx.WXK_RETURN or event.GetKeyCode() == wx.WXK_NUMPAD_ENTER:
            print "enter is pressed"
            t = event.GetEventObject()
            x = t.GetValue()
            t.SetValue("")
            t.AppendText(x)
         else:
             print "enter is not pressed"
             event.Skip()
                   
    ctrlEH = event.GetEventObject().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, HandleEnter)
    event.Skip()

Still it doesn’t accept ENTER key.

···

On Wednesday, April 2, 2014 7:11:58 PM UTC+3, bruce g wrote:

Hi Mike,

Please check out the link I noted.

     [https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J](https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J)

Bruce

On Wednesday, April 2, 2014 11:40:59 AM UTC-4, Mike Driscoll wrote:

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)



def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike

Hi Steve,

I tried my application and enter works.
Your are not exactly following my example.

Binding to the HandleEnter needs to be done outside of on_enter.

Also, I bind using EVT_GRID_EDITOR_CREATED, not EVT_GRID_EDITOR_SHOWN (may be OK).

Bruce

···

On Mon, Apr 7, 2014 at 7:26 AM, steve oslocourse@gmail.com wrote:

Hi Bruce,

I couldn’t get it work, here is what I do:

self.CreateGrid(12, 7)

self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.on_enter)

def on_enter(self, event):
print “editor starts”

    def HandleEnter(event):
         if event.GetKeyCode() == wx.WXK_RETURN or event.GetKeyCode() == wx.WXK_NUMPAD_ENTER:

            print "enter is pressed"

            t = event.GetEventObject()
            x = t.GetValue()

t.SetValue(“”)
t.AppendText(x)

         else:
             print "enter is not pressed"
             event.Skip()
                   
    ctrlEH = event.GetEventObject().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, HandleEnter)

    event.Skip()

Still it doesn’t accept ENTER key.

On Wednesday, April 2, 2014 7:11:58 PM UTC+3, bruce g wrote:

Hi Mike,

Please check out the link I noted.

     [https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J](https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline%2420wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J)

Bruce

On Wednesday, April 2, 2014 11:40:59 AM UTC-4, Mike Driscoll wrote:

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)




def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Sir,

I don’t understand and I don’t know how to bind HandleEnter outside on_enter. Grid event gridlib.EVT_GRID_EDITOR_SHOWN (or gridlib.EVT_GRID_EDITOR_CREATED) happens once on grid. Therefore I pass that event to on_enter(). I don’t know how to use that same event with HandleEnter().

Plus, I can’t find anything about wx.EVT_GRID_EDITOR_CREATED in phoenix documentation.

Best regards

···

On Monday, April 7, 2014 4:29:22 PM UTC+3, bruce g wrote:

Hi Steve,

I tried my application and enter works.
Your are not exactly following my example.

Binding to the HandleEnter needs to be done outside of on_enter.

Also, I bind using EVT_GRID_EDITOR_CREATED, not EVT_GRID_EDITOR_SHOWN (may be OK).

Bruce

On Mon, Apr 7, 2014 at 7:26 AM, steve osloc...@gmail.com wrote:

            t = event.GetEventObject()
            x = t.GetValue()

Hi Bruce,

I couldn’t get it work, here is what I do:

self.CreateGrid(12, 7)

self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.on_enter)

def on_enter(self, event):
print “editor starts”

    def HandleEnter(event):
         if event.GetKeyCode() == wx.WXK_RETURN or event.GetKeyCode() == wx.WXK_NUMPAD_ENTER:

            print "enter is pressed"t.SetValue("")
            t.AppendText(x)

         else:
             print "enter is not pressed"
             event.Skip()
                   
    ctrlEH = event.GetEventObject().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, HandleEnter)

    event.Skip()

Still it doesn’t accept ENTER key.

On Wednesday, April 2, 2014 7:11:58 PM UTC+3, bruce g wrote:

Hi Mike,

Please check out the link I noted.

     [https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J](https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J)

Bruce

On Wednesday, April 2, 2014 11:40:59 AM UTC-4, Mike Driscoll wrote:

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)




def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Hi Steve,

OK, my program was written using wxPython 2.8.12.1

I have not tried it using Phoenix.

I will put together a small program to test using Phoenix.

However, I will not be able to get this until Thursday the earliest.

Bruce

···

On Mon, Apr 7, 2014 at 5:23 PM, steve oslocourse@gmail.com wrote:

Sir,

I don’t understand and I don’t know how to bind HandleEnter outside on_enter. Grid event gridlib.EVT_GRID_EDITOR_SHOWN (or gridlib.EVT_GRID_EDITOR_CREATED) happens once on grid. Therefore I pass that event to on_enter(). I don’t know how to use that same event with HandleEnter().

Plus, I can’t find anything about wx.EVT_GRID_EDITOR_CREATED in phoenix documentation.

Best regards

On Monday, April 7, 2014 4:29:22 PM UTC+3, bruce g wrote:

Hi Steve,

I tried my application and enter works.
Your are not exactly following my example.

Binding to the HandleEnter needs to be done outside of on_enter.

Also, I bind using EVT_GRID_EDITOR_CREATED, not EVT_GRID_EDITOR_SHOWN (may be OK).

Bruce

On Mon, Apr 7, 2014 at 7:26 AM, steve osloc...@gmail.com wrote:

Hi Bruce,

I couldn’t get it work, here is what I do:

self.CreateGrid(12, 7)

self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.on_enter)

def on_enter(self, event):
print “editor starts”

    def HandleEnter(event):
         if event.GetKeyCode() == wx.WXK_RETURN or event.GetKeyCode() == wx.WXK_NUMPAD_ENTER:


            print "enter is pressed"

            t = event.GetEventObject()
            x = t.GetValue()

t.SetValue(“”)
t.AppendText(x)

         else:
             print "enter is not pressed"
             event.Skip()
                   
    ctrlEH = event.GetEventObject().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, HandleEnter)


    event.Skip()

Still it doesn’t accept ENTER key.

On Wednesday, April 2, 2014 7:11:58 PM UTC+3, bruce g wrote:

Hi Mike,

Please check out the link I noted.

     [https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J](https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/newline%2420wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J)

Bruce

On Wednesday, April 2, 2014 11:40:59 AM UTC-4, Mike Driscoll wrote:

On Wednesday, April 2, 2014 10:34:18 AM UTC-5, bruce g wrote:

Steve,

You would expect a widget as powerful as wx.grid would make handling the Enter key simple.

In any case, the below snippet is what I use.
For more info:
https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

                  newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

Bruce

 ctrlEH = event.GetControl().GetEventHandler()
 ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)





def handleKey(self, event):
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')

I think you’re eating the “ENTER” key right here. You need to add an event.Skip() here as well.

  • Mike

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Hi Steve,

Below is a small program to show how to insert a carriage return in a wx.grid cell.
I have tested this program using wxPython 2.8.10.1
Perhaps you can try it using Phoenix (I will do this later this week).
Also, wx.EVT_GRID_EDITOR_CREATED is listed in https://github.com/wxWidgets/Phoenix/blob/master/samples/grid/events.py

Bruce

Small program to show how to insert a carriage return in a wx.grid cell. (BG 07-APR-2014)

···

See https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

import wx
import wx.grid as gridlib
import winsound

class GridEnterTest(wx.Frame):
def init(self, title=‘Grid Enter Test’):
wx.Frame.init(self, None, wx.ID_ANY, title)

    self.panel = wx.Panel(self, wx.ID_ANY)
    self.grid = gridlib.Grid(self.panel)
   
    self.grid.CreateGrid(3,3)
    self.SetSize ((400,400))
    self.grid.SetSize ((400,400))
   
    self.attribReadWriteLeft = self.setCellAttribReadWrite()
    for i in range (self.grid.GetNumberCols()):
        self.grid.SetColAttr(i, self.attribReadWriteLeft)  
       
    for i in range (self.grid.GetNumberRows()):
        self.grid.SetRowSize(i, 100)
    self.grid.SetRowMinimalAcceptableHeight(100)
   
    self.grid.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.gridEditorCreated)
   
    self.grid.AutoSizeRows()
    self.grid.ForceRefresh() 
    self.Show()  
         

def setCellAttribReadWrite(self, align="LEFT", bcolor="WHITE"):
    attrib = gridlib.GridCellAttr()
    attrib.SetFont(wx.Font(8, wx.ROMAN, wx.NORMAL, wx.NORMAL,False,'Tahoma'))
    if align.upper() == "LEFT":
        attrib.SetAlignment(wx.ALIGN_LEFT, wx.ALIGN_LEFT) 
    else:
        attrib.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) 
    #attrib.SetRenderer(GridCellWrap.CustomGridCellAutoWrapStringRenderer())
    attrib.SetEditor(wx.grid.GridCellAutoWrapStringEditor())
    attrib.SetBackgroundColour(bcolor)       
    attrib.IncRef() 
    return attrib 
   

def gridEditorCreated(self, event):         
    ctrlEH = event.GetControl().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)

def handleKey(self, event):
    INPUT_LIMIT = 50
    BEEP_FREQUENCY = 1500  #in hertz
    BEEP_DURATION  = 1000  #in ms 
      
    newKey = event.KeyCode
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')
      
    else:
        if (newKey != wx.WXK_DELETE) and (newKey != wx.WXK_BACK):
            #do not check if the user typed delete or backspace!
            #A Grid cell is made up of a TextCtrl.
            #Use TextCtrl methods to limit cell length.
            t = event.GetEventObject()
            x = t.GetValue()
            charCount = len(x)
            if charCount >= INPUT_LIMIT:
                #the max status field length has been exceeded
                data = x[0:INPUT_LIMIT]
                winsound.Beep(BEEP_FREQUENCY, BEEP_DURATION) #alert the user length exceeded
                t.SetValue("")
                t.AppendText(data)
            else:
                event.Skip()  #display the new key normally
        else:
            event.Skip()  #process the backspace or delete keys normally 

if name == ‘main’:
app = wx.App(False)
frame = GridEnterTest()
frame.Show()
app.MainLoop()

END

Sir,

Thank you for the example,

Your grid can check each ENTER event, for example you write “abc”, press ENTER (it adds a new line), then type “def”, press ENTER (it adds another new line)

However my grid doesn’t do that (although I used the same code as yours), that is: I want to type number “123” in the cell. When I type the first digit (number “1”), it sees that ENTER is not pressed and skips the event. I type the rest “23” and it no longer checks ENTER. When I press ENTER in the end, nothing happens. Here is my code (please note I dont use custom setCellAttribReadWrite unlike yours):

self.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.gridEditorCreated)

def gridEditorCreated(self, event):        
    ctrlEH = event.GetControl().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, self.on_enter)        
   
def on_enter(self, event):
    print "editor starts"
    newKey = event.KeyCode
   
    if newKey == wx.WXK_RETURN or newKey == wx.WXK_NUMPAD_ENTER:
            print "enter is pressed"
            t = event.GetEventObject()
            x = t.GetValue()
            t.SetValue("")
            t.AppendText(x)
    else:
            print "enter is not pressed"
            event.Skip()
                   

    event.Skip()
···

On Tuesday, April 8, 2014 2:51:21 AM UTC+3, bruce g wrote:

Hi Steve,

Below is a small program to show how to insert a carriage return in a wx.grid cell.
I have tested this program using wxPython 2.8.10.1
Perhaps you can try it using Phoenix (I will do this later this week).
Also, wx.EVT_GRID_EDITOR_CREATED is listed in https://github.com/wxWidgets/Phoenix/blob/master/samples/grid/events.py

Bruce

Small program to show how to insert a carriage return in a wx.grid cell. (BG 07-APR-2014)

See https://groups.google.com/forum/?hl=en&fromgroups=#!searchin/wxpython-users/

newline$20wx.grid/wxpython-users/bMiG-jN03k4/uf4svkiAlJ0J

import wx
import wx.grid as gridlib
import winsound

class GridEnterTest(wx.Frame):
def init(self, title=‘Grid Enter Test’):
wx.Frame.init(self, None, wx.ID_ANY, title)

    self.panel = wx.Panel(self, wx.ID_ANY)
    self.grid = gridlib.Grid(self.panel)
   
    self.grid.CreateGrid(3,3)
    self.SetSize ((400,400))
    self.grid.SetSize ((400,400))
   
    self.attribReadWriteLeft = self.setCellAttribReadWrite()
    for i in range (self.grid.GetNumberCols()):
        self.grid.SetColAttr(i, self.attribReadWriteLeft)  
       
    for i in range (self.grid.GetNumberRows()):
        self.grid.SetRowSize(i, 100)
    self.grid.SetRowMinimalAcceptableHeight(100)
   
    self.grid.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.gridEditorCreated)
   
    self.grid.AutoSizeRows()
    self.grid.ForceRefresh() 
    self.Show()  
         

def setCellAttribReadWrite(self, align="LEFT", bcolor="WHITE"):
    attrib = gridlib.GridCellAttr()
    attrib.SetFont(wx.Font(8, wx.ROMAN, wx.NORMAL, wx.NORMAL,False,'Tahoma'))
    if align.upper() == "LEFT":
        attrib.SetAlignment(wx.ALIGN_LEFT, wx.ALIGN_LEFT) 
    else:
        attrib.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) 
    #attrib.SetRenderer(GridCellWrap.CustomGridCellAutoWrapStringRenderer())
    attrib.SetEditor(wx.grid.GridCellAutoWrapStringEditor())
    attrib.SetBackgroundColour(bcolor)       
    attrib.IncRef() 
    return attrib 
   

def gridEditorCreated(self, event):         
    ctrlEH = event.GetControl().GetEventHandler()
    ctrlEH.Bind(wx.EVT_KEY_DOWN, self.handleKey)


def handleKey(self, event):
    INPUT_LIMIT = 50
    BEEP_FREQUENCY = 1500  #in hertz
    BEEP_DURATION  = 1000  #in ms 
      
    newKey = event.KeyCode
    if newKey == wx.WXK_RETURN:
        #insert a carriage return in the status field
        event.GetEventObject().WriteText('\n')
      
    else:
        if (newKey != wx.WXK_DELETE) and (newKey != wx.WXK_BACK):
            #do not check if the user typed delete or backspace!
            #A Grid cell is made up of a TextCtrl.
            #Use TextCtrl methods to limit cell length.
            t = event.GetEventObject()
            x = t.GetValue()
            charCount = len(x)
            if charCount >= INPUT_LIMIT:
                #the max status field length has been exceeded
                data = x[0:INPUT_LIMIT]
                winsound.Beep(BEEP_FREQUENCY, BEEP_DURATION) #alert the user length exceeded
                t.SetValue("")
                t.AppendText(data)
            else:
                event.Skip()  #display the new key normally
        else:
            event.Skip()  #process the backspace or delete keys normally 

if name == ‘main’:
app = wx.App(False)
frame = GridEnterTest()
frame.Show()
app.MainLoop()

END

Hi Steve,

I updated your code.
Also, it seems to want to have the cell attribute set.

Bruce

def on_enter(self, event):
print “editor starts”
newKey = event.KeyCode

    if newKey == wx.WXK_RETURN or newKey == wx.WXK_NUMPAD_ENTER:
            print "enter is pressed"
            event.GetEventObject().WriteText('\n')

            t = event.GetEventObject()
            x = t.GetValue()
            t.SetValue("")
            t.AppendText(x)
    else:
            print "enter is not pressed"
            event.Skip()
···

END

Hi Bruce,

It still doesnt work,

I don’t want to add a newline when I press ENTER. I want the cell to accept the value that I type when I press ENTER.

For example I started the grid editor on a cell, typed number 123, and when I press ENTER it should save 123 in cell and editor should end.
I forgot to tell that I use gridlib.GridCellNumberEditor:

editorNum = gridlib.GridCellNumberEditor(0,1000)
editorNum.SetParameters(‘0,1000’)
for row in xrange(12*7):
self.SetCellEditor(row, 2, editorNum)

···

On Wednesday, April 9, 2014 3:08:43 PM UTC+3, bruce g wrote:

Hi Steve,

I updated your code.
Also, it seems to want to have the cell attribute set.

Bruce

def on_enter(self, event):
print “editor starts”
newKey = event.KeyCode

    if newKey == wx.WXK_RETURN or newKey == wx.WXK_NUMPAD_ENTER:
            print "enter is pressed"
            event.GetEventObject().WriteText('\n')

            t = event.GetEventObject()
            x = t.GetValue()
            t.SetValue("")
            t.AppendText(x)
    else:
            print "enter is not pressed"
            event.Skip()

END

Hi Steve,

OK, now I understand that you are using gridlib.GridCellNumberEditor(0,1000)

The solution can be found in the the wxPython demo.
Choose the “Grid” control, “wx.Grid showing Editors and Renderers.”
Enter data for “GridCellNumberEditor with min,max” followed by the Enter key.
Does this meet your needs?

Bruce

Hi Bruce,
I checked that demo, * “GridCellNumberEditor with min,max”* doesn’t accept ENTER key either.
However, in that demo, there is a cell “GridCellNumberRenderer GridCellNumberEditor”, it only allows numbers to be entered and it also accepts ENTER key. I think this is what I’m looking for.
But, still, I would like to know how to make gridlib.GridCellNumberEditor accept ENTER key.

Best regards

···

On Thursday, April 10, 2014 2:37:06 AM UTC+3, bruce g wrote:

Hi Steve,

OK, now I understand that you are using gridlib.GridCellNumberEditor(0,1000)

The solution can be found in the the wxPython demo.
Choose the “Grid” control, “wx.Grid showing Editors and Renderers.”
Enter data for “GridCellNumberEditor with min,max” followed by the Enter key.
Does this meet your needs?

Bruce

steve wrote:

Hi Bruce,

I checked that demo,/*"GridCellNumberEditor with min,max"*/ doesn't
accept ENTER key either.
However, in that demo, there is a cell/*"GridCellNumberRenderer
GridCellNumberEditor"*/, it only allows numbers to be entered and it
also accepts ENTER key. I think this is what I'm looking for.

But, still, I would like to know how to make
/*gridlib.GridCellNumberEditor*/ accept ENTER key.

What platform? It may be that the native spinctrl is eating or blocking the key events for the enter key.

···

--
Robin Dunn
Software Craftsman

Hi

It is Windows 7 with wx 2.9.5.0

Best regards

···

On Friday, April 11, 2014 3:38:40 AM UTC+3, Robin Dunn wrote:

steve wrote:

Hi Bruce,

I checked that demo,/“GridCellNumberEditor with min,max”/ doesn’t

accept ENTER key either.

However, in that demo, there is a cell/*"GridCellNumberRenderer

GridCellNumberEditor"*/, it only allows numbers to be entered and it

also accepts ENTER key. I think this is what I’m looking for.

But, still, I would like to know how to make

/gridlib.GridCellNumberEditor/ accept ENTER key.

What platform? It may be that the native spinctrl is eating or blocking
the key events for the enter key.


Robin Dunn

Software Craftsman

http://wxPython.org