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?
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?
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
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.
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')
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.
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:
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.
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:
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.
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).
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.
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).
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:
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.
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)
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()
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):
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)
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()
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()
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.
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()
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?
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?
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.