WXgrid 拖放事件 - 如何计算类上的滚动位置

发布于 2025-01-11 09:33:01 字数 2884 浏览 0 评论 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 中调用的单独函数中...

感谢您的任何指示

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-01-18 09:33:01

万一其他人在这里绊倒,这里是修复(传递父级):

class GridFileDropTarget(wx.FileDropTarget):
    def __init__(self, parent, grid, *args, **kwargs):
        wx.FileDropTarget.__init__(self, *args, **kwargs)
        self.grid = grid
        self.parent = parent

    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()
            return(1)
        else :
            return(0)

并调用为:

        dropTarget = GridFileDropTarget(self, self.myGrid)   #link the 2 classes

In case anyone else stumbles here, here's the fix (passing parent):

class GridFileDropTarget(wx.FileDropTarget):
    def __init__(self, parent, grid, *args, **kwargs):
        wx.FileDropTarget.__init__(self, *args, **kwargs)
        self.grid = grid
        self.parent = parent

    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()
            return(1)
        else :
            return(0)

and call as:

        dropTarget = GridFileDropTarget(self, self.myGrid)   #link the 2 classes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文