WXgrid 拖放事件 - 如何计算类上的滚动位置
当我尝试在面板中的子网格上实现拖放功能时,我一直试图获取未滚动的位置。当我尝试拖放时,我可以从类内取消滚动(请参阅 onMouseOver),但不能跨类滚动,返回
File "dnd2.py", line 11, in OnDropFiles
x, y = self.grid.CalcUnscrolledPosition(x, y)
AttributeError: 'EditDialog' object has no attribute 'CalcUnscrolledPosition'
以下是显示问题的精简可运行版本:
import wx
import wx.grid as gridlib
class GridFileDropTarget(wx.FileDropTarget):
def __init__(self, grid):
wx.FileDropTarget.__init__(self)
self.grid = grid
def OnDropFiles(self, x, y, filenames):
# the x,y coordinates here are Unscrolled coordinates. Change to scrolled coordinates.
x, y = self.grid.CalcUnscrolledPosition(x, y)
# now we need to get the row and column from the grid
col = self.grid.XToCol(x)
row = self.grid.YToRow(y)
if row > -1 and col > -1:
self.grid.SetCellValue(row, col, filenames[0])
self.grid.AutoSizeColumn(col)
self.grid.Refresh()
class EditDialog(wx.Dialog):
#----------------------------------------------------------------------
def __init__(self, *args, **kw):
super(EditDialog, self).__init__(*args, **kw)
self.InitUI()
self.SetTitle("Edit Entry")
def InitUI(self):
wx.Dialog.__init__ (self, None, id = wx.ID_ANY, title = u"Edit Entry", pos = wx.DefaultPosition, style = wx.DEFAULT_DIALOG_STYLE )
self.panel = wx.Panel(self, wx.ID_ANY)
self.myGrid = gridlib.Grid(self.panel)
self.myGrid.CreateGrid(15, 5)
#bind mouse voer tool tip for drag and drop
self.myGrid.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)
#set up drag n drop
self.moveTo = None
dropTarget = GridFileDropTarget(self)
self.myGrid.SetDropTarget(dropTarget)
self.myGrid.EnableDragRowSize()
self.myGrid.EnableDragColSize()
#define sizers
topSizer = wx.BoxSizer(wx.VERTICAL)
Gridsizer = wx.BoxSizer(wx.VERTICAL)
Gridsizer.Add(self.myGrid)
topSizer.Add(Gridsizer, 0 , wx.ALL|wx.EXPAND)
self.panel.SetSizer(topSizer)
topSizer.Fit(self)
def onMouseOver(self, event):
x, y = self.myGrid.CalcUnscrolledPosition(event.GetX(),event.GetY())
coords = self.myGrid.XYToCell(x, y)
col = coords[1]
row = coords[0]
if col == 2 or col == 3:
msg = "Drag and Drop files here"
self.myGrid.GetGridWindow().SetToolTip(msg)
else:
self.myGrid.GetGridWindow().SetToolTip('')
if __name__ == '__main__':
import sys
app = wx.App()
dlg = EditDialog()
res = dlg.ShowModal()
app.MainLoop()
我尝试了各种方法来解决 self.grid。 CalcUnScrolled 但无济于事 - 这是因为我将所有内容都集中在 def InitUI 下,而不是在我可以在 self.grid 中调用的单独函数中...
感谢您的任何指示
I'm stuck trying to get the unscrolled position when I try and implement a drag ond drop function onto a subgrid in a panel. I can get the unscrolled from within the class (see onMouseOver) but not across classes when I try the drag and drop which returns
File "dnd2.py", line 11, in OnDropFiles
x, y = self.grid.CalcUnscrolledPosition(x, y)
AttributeError: 'EditDialog' object has no attribute 'CalcUnscrolledPosition'
Here's the cut down runable version that shows the issue:
import wx
import wx.grid as gridlib
class GridFileDropTarget(wx.FileDropTarget):
def __init__(self, grid):
wx.FileDropTarget.__init__(self)
self.grid = grid
def OnDropFiles(self, x, y, filenames):
# the x,y coordinates here are Unscrolled coordinates. Change to scrolled coordinates.
x, y = self.grid.CalcUnscrolledPosition(x, y)
# now we need to get the row and column from the grid
col = self.grid.XToCol(x)
row = self.grid.YToRow(y)
if row > -1 and col > -1:
self.grid.SetCellValue(row, col, filenames[0])
self.grid.AutoSizeColumn(col)
self.grid.Refresh()
class EditDialog(wx.Dialog):
#----------------------------------------------------------------------
def __init__(self, *args, **kw):
super(EditDialog, self).__init__(*args, **kw)
self.InitUI()
self.SetTitle("Edit Entry")
def InitUI(self):
wx.Dialog.__init__ (self, None, id = wx.ID_ANY, title = u"Edit Entry", pos = wx.DefaultPosition, style = wx.DEFAULT_DIALOG_STYLE )
self.panel = wx.Panel(self, wx.ID_ANY)
self.myGrid = gridlib.Grid(self.panel)
self.myGrid.CreateGrid(15, 5)
#bind mouse voer tool tip for drag and drop
self.myGrid.GetGridWindow().Bind(wx.EVT_MOTION, self.onMouseOver)
#set up drag n drop
self.moveTo = None
dropTarget = GridFileDropTarget(self)
self.myGrid.SetDropTarget(dropTarget)
self.myGrid.EnableDragRowSize()
self.myGrid.EnableDragColSize()
#define sizers
topSizer = wx.BoxSizer(wx.VERTICAL)
Gridsizer = wx.BoxSizer(wx.VERTICAL)
Gridsizer.Add(self.myGrid)
topSizer.Add(Gridsizer, 0 , wx.ALL|wx.EXPAND)
self.panel.SetSizer(topSizer)
topSizer.Fit(self)
def onMouseOver(self, event):
x, y = self.myGrid.CalcUnscrolledPosition(event.GetX(),event.GetY())
coords = self.myGrid.XYToCell(x, y)
col = coords[1]
row = coords[0]
if col == 2 or col == 3:
msg = "Drag and Drop files here"
self.myGrid.GetGridWindow().SetToolTip(msg)
else:
self.myGrid.GetGridWindow().SetToolTip('')
if __name__ == '__main__':
import sys
app = wx.App()
dlg = EditDialog()
res = dlg.ShowModal()
app.MainLoop()
I've tried various ways to address the self.grid.CalcUnScrolled but to no avail - it is because of the way I have everything lumped under def InitUI rather than in a separate function that I could call in the self.grid...
thanks for any pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
万一其他人在这里绊倒,这里是修复(传递父级):
并调用为:
In case anyone else stumbles here, here's the fix (passing parent):
and call as: