问题描述
当用户将一列拖到新索引时,会触发wx.grid.EVT_GRID_COL_MOVE
事件.处理程序接收一个 wx.grid.GridEvent
,其中包含属性 Col
,其中包含移动列的旧索引.不过,该事件似乎不包含任何详细说明列已移动到到 的位置的属性.我该如何解决?
When the user drags a column to a new index, the wx.grid.EVT_GRID_COL_MOVE
event is triggered. The handler receives a wx.grid.GridEvent
which contains the property Col
which contains the old index of the moved column. The event doesn't appear to contain any attributes detailing where the column has been moved to though. How do I figure that out?
推荐答案
对于EVT_GRID_COL_MOVE
事件,GridEvent.GetCol()
返回ID 正在移动的列.列的实际位置可以通过Grid.GetColPos(colId)
获得.然而,wxWidgets 2.8 和 wx 2.9 之间存在差异:
In case of theEVT_GRID_COL_MOVE
event, GridEvent.GetCol()
returns the ID of the column that is being moved. The actual position of the column can be obtained with Grid.GetColPos(colId)
. However,there's a difference between wxWidgets 2.8 and wx 2.9:
在 wx 2.8
中,EVT_GRID_COL_MOVE
在列移动后 发送,这将阻止您否决该事件.因此在事件期间调用 GetColPos()
将返回列的新位置.
In wx 2.8
, EVT_GRID_COL_MOVE
is sent after the column has been moved which would prevent you from vetoing the event.Therefore calling GetColPos()
during the event will return the new position of the column.
在wx 2.9
中,EVT_GRID_COL_MOVE
在列被移动之前被触发,否决事件将阻止列被移动.在事件期间调用 GetColPos()
将返回列的当前位置.
In wx 2.9
, EVT_GRID_COL_MOVE
is triggered before the column is moved and vetoing the event will prevent the column from being moved.Calling GetColPos()
during the event will return the current position of the column.
列的新位置是根据鼠标位置在内部计算的.在 python 中复制该代码可能会与可能破坏程序的未来版本发生冲突.
The new position of the column is calculated internally, based on the mouse position.Duplicating that code in python may conflict with future releases which may break the program.
使用 wx 2.9+
,您可以实现一种变通方法,该方法将为您提供新的列位置并允许您否决"(或更确切地说撤消)移动:
Using wx 2.9+
you can implement a workaround that will give you the new column position and allows you to 'veto' (or rather undo) the move:
self.Bind(wx.grid.EVT_GRID_COL_MOVE, self.OnColMove)
def OnColMove(self,evt):
colId = evt.GetCol()
colPos = self.grid.GetColPos(colId)
wx.CallAfter(self.OnColMoved,colId,colPos)
# allow the move to complete
def OnColMoved(self,colId,oldPos):
# once the move is done, GetColPos() returns the new position
newPos = self.grid.GetColPos(colId)
print colId, 'from', oldPos, 'to', newPos
undo = False
if undo: # undo the move (as if the event was veto'd)
self.grid.SetColPos(colId,oldPos)
请注意,如果必须撤消移动,则 SetColPos
将被调用两次(一次在内部,第二次在 OnColMoved
中).
Note that SetColPos
will be called twice if the move has to be undone (once internally and a second time in OnColMoved
).
或者你可以查看wx.lib.gridmovers
.
这篇关于wxPython - wxGrid - 如何检测哪一列移动到哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!