I am trying to add a button to a grid cell.
I am using latest demo 4.0.7 and python 3.7 (32bit)
I fixed an old example found here: https://groups.google.com/forum/#!topic/wxpython-users/HhtKCxPVX_s
The problem is the button is not working correctly and pressing it causing the whole grid to disappear.
class MyCustomRenderer2(gridlib.GridCellRenderer):
def __init__(self):
gridlib.GridCellRenderer.__init__(self)
self.down = False
self.click_handled = False
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
"""This is called when the widget is Refreshed"""
print ('drawing button')
dc.Clear()
if self.down:
state = wx.CONTROL_PRESSED | wx.CONTROL_SELECTED
else:
state = 0
#if not self.IsEnabled():
# state = wx.CONTROL_DISABLED
#pt = self.ScreenToClient(wx.GetMousePosition())
#if self.GetClientRect().Contains(pt):
# state |= wx.CONTROL_CURRENT
wx.RendererNative.Get().DrawPushButton(grid, dc, rect, state)
#extra logic required since a button gets drawn at various times that could be while the mouse button is held down
if self.down and not self.click_handled:
self.click_handled = True
self.HandleClick()
def HandleClick(self):
print ('clicked')
def GetBestSize(self, grid, attr, dc, row, col):
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
w, h = dc.GetTextExtent(text)
return wx.Size(w, h)
def Clone(self):
return MyCustomRenderer2()