zoukankan      html  css  js  c++  java
  • wxpython实现文件拖拽

    我想让wx.grid里面的单元格能够支持文件拖拽,实现起来挺简单的,共分3步:

    1、创建一个wx.FileDropTarget子类的对象,并把要支持拖拽的控件传给它的构造函数,此处是grid

    2、调用grid的SetDropTarget函数,并将第1步创建的wx.FileDropTarget子类对象传给它

    3、实现第1步创建的wx.FileDropTarget的子类,并覆盖OnDropFiles函数

    贴出代码如下:

    import wx
    import wx.grid
    import logging
    import os
    
    # 第3步,实现wx.FileDropTarget子类
    class FileDrop(wx.FileDropTarget):
        def __init__(self, grid):
            wx.FileDropTarget.__init__(self)
            self.grid = grid
    
        def OnDropFiles(self, x, y, filePath):         # 当文件被拖入grid后,会调用此方法
            cellCoords = self.grid.XYToCell(x, y)      # 根据坐标轴换算被拖入grid网格的行号和列号
            filename = os.path.basename(filePath[0])
            self.grid.SetCellValue(cellCoords.GetRow(), cellCoords.GetCol(), filename)  # 将文件名赋给被拖入的cell
            
    
    class MyFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, 'MyFrame', size = (640, 480))
            panel = wx.Panel(self, -1)
    
            vSizer = wx.BoxSizer(wx.VERTICAL)
            self.grid = wx.grid.Grid(panel, -1)
            self.grid.CreateGrid(10, 3)
            
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.grid, 1, wx.ALL | wx.EXPAND, 5)
            vSizer.Add(sizer, 1, wx.ALL | wx.EXPAND)
    
            panel.SetSizer(vSizer)
            self.fileDrop = FileDrop(self.grid)      # 第1步,创建FileDrop对象,并把grid传给初始化函数
            self.grid.SetDropTarget(self.fileDrop)   # 第2步,调用grid的SetDropTarget函数,并把FileDrop对象传给它
    
    class MainApp(wx.App):
        def __init__(self, redirect = False, filename = None):
            wx.App.__init__(self, redirect, filename)
    
        def OnInit(self):
            self.frame = MyFrame()
            self.frame.Show()
            self.frame.Center()
            return True
    
    app = MainApp()
    app.MainLoop()
  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/palance/p/4815065.html
Copyright © 2011-2022 走看看