0

当我尝试将拖放功能实现到面板中的子网格上时,我一直试图获得未滚动的位置。当我尝试返回的拖放操作时,我可以从类中获取未滚动的内容(请参阅 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 中调用的单独函数中......

感谢您的任何指点

4

0 回答 0