Reputation: 13133
I am trying to figure out how to eliminate drag behavior that is the default in a wx.Grid. I would like to end up with a grid for which the user cannot drag at all.
I (eventually) found MouseEventsManager, which looked ideal but doesn't do what I expect:
import wx
import wx.grid as gridlib
class GridMouseEventsManager(wx.MouseEventsManager):
def MouseDragBegin(self, item, pos):
return False
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Sample grid")
self.grid = gridlib.Grid(self)
self.grid.CreateGrid(5,4)
self.grid.SetCellSize(4,1,1,2)
self.grid.SetColLabelSize(0) # eliminates spreadsheet-style row & col headers
self.grid.SetRowLabelSize(0)
rowHeight = 50
colWidth = 50
for i in range(1,5):
self.grid.SetRowSize(i, rowHeight)
for i in range(0,4):
self.grid.SetColSize(i, colWidth)
self.grid.SetCellBackgroundColour(4, 3, wx.LIGHT_GREY)
# self.grid.SetCellHighlightPenWidth(0) # eliminates highlighted cell, i.e., 'current' cell in spreadsheet
self.grid.EnableCellEditControl(False)
self.grid.Bind(gridlib.EVT_GRID_SELECT_CELL, self.GridSelect, self.grid)
self.grid_mouse_events_manager = GridMouseEventsManager(self.grid)
self.Bind(wx.EVT_CLOSE, self.on_exit)
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.grid, 0, wx.ALIGN_CENTER)
self.selectedRow = -1
self.selectedCol = -1
self.SetSizerAndFit(mainSizer)
# self.ShowFullScreen(True)
def GridSelect(self, event):
col = event.GetCol()
row = event.GetRow()
penWidth = 0
if col == 1:
penWidth = 2
self.grid.SetCellHighlightPenWidth(penWidth)
self.Refresh()
def on_exit(self, event):
self.grid_mouse_events_manager.Destroy()
self.Destroy()
# Run the program
if __name__ == "__main__":
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
This still has the default behavior, which is to select multiple cells when the user clicks and drags. I would like to disable dragging and/or multiple cell selection entirely.
I'm fairly new to Python, and even newer to wxPython; I haven't been able to find an explanation or example of how to do this. I tried the MouseEventsManager constructor with both the self
object and with the grid
object, neither of which seem to have any effect.
How is this done?
Upvotes: 0
Views: 433
Reputation: 13133
Well, wouldn't you know that one more search on wxpython.org produced what I wanted
self.grid.GetGridWindow().Bind(wx.EVT_MOTION, self.OnMotion)
...
def OnMotion(self, event)
# just trap this event and prevent it from percolating up the window hierarchy
pass
That did what I want.
Upvotes: 1