【问题标题】:Drag and drop image in Wxpython在 Wxpython 中拖放图像
【发布时间】:2021-02-11 04:40:10
【问题描述】:

我在 wxpython 中创建了 3 个面板。一个在上面。底部的其他 2 个面板以垂直方式排列。 Panel2 由 ListControl 作为 list1 组成,Panel3 由 ListControl 作为 list2 组成。

我在 panel2 和 panel3 中将 wx.ImageList 与 wx.ListCtrl 结合使用。

我正在尝试将图像从 Panel2 拖到 Panel3。 用过的: self.list1.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit, id=self.list1.GetId())

我在def OnDragInit函数中使用wx.FileDropTarget定义了Drop目标和wx.BitmapDataObject

一段代码如下:

class MyTarget(wx.FileDropTarget):
    def __init__(self, object):
        wx.FileDropTarget.__init__(self)
        self.object = object

    def OnDropFiles(self, x, y, filenames):
        print(filenames)
        return(True)

    def OnDragInit(self, event):
        text = self.list1.GetBitmap(event.GetIndex())
        too = wx.BitmapDataObject(text)
        src = wx.DropSource(self.list2)
        src.SetData(too)
        src.DoDragDrop(True)

问题:放置目标不接受数据。

【问题讨论】:

    标签: user-interface drag-and-drop wxpython drag drop


    【解决方案1】:

    我怀疑你没有设置SetDropTarget

    请看这里,FileDrTr = MyFileDropTarget(self)Drop_Place.SetDropTarget(FileDrTr)

    import wx
    
    import wx.lib.newevent
    drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
    
    class MyFileDropTarget(wx.FileDropTarget):
        def __init__(self, obj):
            wx.FileDropTarget.__init__(self)
            self.obj = obj
    
        def OnDropFiles(self, x, y, filename):
            #filename is a list of 1 or more files
            #here we are restricting it 1 file by only taking the first item of the list
            TempTxt = filename[0]
            evt = drop_event(data=TempTxt)
            wx.PostEvent(self.obj,evt)
    
            return True
    
    
    class Example(wx.Frame):
        def __init__(self, parent, title):
            super(Example, self).__init__(parent, title=title)
            self.InitUI()
            self.Center()
    
        def InitUI(self):
            panel = wx.Panel(self)
            FileDrTr = MyFileDropTarget(self)
            font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
            font.SetPointSize(9)
            verBox = wx.BoxSizer(wx.VERTICAL)
            horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
            TextLabel = wx.StaticText(panel, label = 'Drop file here')
            TextLabel.SetFont(font)
            horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
            Drop_Place = wx.TextCtrl(panel)
            Drop_Place.SetDropTarget(FileDrTr)
    
            #Bind the drop event listener
            self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)
    
    
            horBoxOne.Add(Drop_Place, proportion=1)
            verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
            verBox.Add((-1, 10))
            horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
            self.NewText = wx.StaticText(panel, label = 'Path will be')
            horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
            verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
            panel.SetSizer(verBox)
    
        def LabelTextUpdate(self, event):
            txt = event.data
            self.NewText.SetLabel(txt)
    
    
    def main():
        app = wx.App()
        ex = Example(None, title = 'drop and see file path')
        ex.Show()
        app.MainLoop()
    
    if __name__ == '__main__':
        main()
    

    注意target 是textctrl,将文件放到面板上不会有任何效果,而放到target(称为Drop_Place 的textctrl)上会激活事件。

    【讨论】:

    • 以上代码适用于文件。我想将图像 .png/.bmp/.jpg/.jpeg 从一个面板拖放到另一个面板。这就是我面临问题的地方。
    • 您放置在第一个面板中的图像,几乎可以肯定是作为一个文件开始的,您将其转换为 wx.Bitmap 以放置到面板中。给定文件名,为什么不能再次应用相同的转换?
    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2023-03-03
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多