wxPython将文件拖放到图像上,添加到数组中

问题描述 投票:0回答:1

我有一个简单的wxPython应用程序,带有1个图像“在这里删除文件!”和2个按钮。

我希望用户能够将文件拖放到顶部/图像上,此时图像会发生变化,文件会被加载到数组中。

这就是我所需要的一切,但是我遇到了一个重大的障碍,让拖累工作得以实现。有人可以看看我的代码,并找出如何/在哪里集成拖放事件?任何帮助都会很棒。

UI image

import wx

class DropTarget(wx.FileDropTarget):

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

        image = Image.open(filenames[0])
        image.thumbnail((PhotoMaxSize, PhotoMaxSize))
        image.save('thumbnail.png')
        pub.sendMessage('dnd', filepath='thumbnail.png')
        return True

    def __init__(self, parent, ID, title):
        wx.FileDropTarget.__init__(self, parent, ID, title, size=(300, 340), style= wx.CLOSE_BOX)
        #self.widget = widget

class MyFrame(wx.Frame):

    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title, size=(300, 340), style= wx.CLOSE_BOX)


        panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
        panel2 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)

        panel1.SetBackgroundColour("BLUE")
        panel2.SetBackgroundColour("RED")

        image_file = 'bgimage1.png'
        bmp1 = wx.Image(
            image_file,
            wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        # image's upper left corner anchors at panel
        # coordinates (0, 0)
        self.bitmap1 = wx.StaticBitmap(
            self, -1, bmp1, (0, 0))
        # show some image details
        str1 = "%s  %dx%d" % (image_file, bmp1.GetWidth(),
                          bmp1.GetHeight())

        # button
        closeButton = wx.Button(self.bitmap1, label='Generate', pos=(30, 280))
        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

        clearButton = wx.Button(self.bitmap1, label='Clear', pos=(170, 280))
        clearButton.Bind(wx.EVT_BUTTON, self.OnClose)


        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(panel1, 5, wx.EXPAND)
        box.Add(panel2, 1, wx.EXPAND)

        self.SetAutoLayout(True)
        self.SetSizer(box)
        self.Layout()

    def OnDropFiles(self, x, y, filenames):
        self.window.updateDisplay(filenames)
        for name in filenames:
            self.window.WriteText(name + "\n")
            print(name)

        return True

    def OnClose(self, e):
        self.Close(True)

app = wx.App()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()
python wxpython
1个回答
0
投票

在dropfiles之后,你将类DropTarget带回init。您还需要将图像和按钮放在其中一个面板上。 见下文:

import wx

class DropTarget(wx.FileDropTarget):
    def __init__(self, obj):
        wx.FileDropTarget.__init__(self)
        self.obj = obj

    def OnDropFiles(self, x, y, filenames):
        print("Drop Event",filenames)

#        image = Image.open(filenames[0])
#        image.thumbnail((PhotoMaxSize, PhotoMaxSize))
#        image.save('new.png')
#        pub.sendMessage('dnd', filepath='new.png')
        return True

class MyFrame(wx.Frame):

    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title, size=(300, 340))


        panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
        panel2 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)

        panel1.SetBackgroundColour("BLUE")
        panel2.SetBackgroundColour("RED")

        image_file = 'bgimage1.png'
        bmp1 = wx.Image(image_file,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap1 = wx.StaticBitmap(panel1, -1, bmp1, (0, 0))

        # button
        closeButton = wx.Button(panel2, -1, label='Generate',pos=(30, 280))
        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

        clearButton = wx.Button(panel2, -1, label='Clear',pos=(170, 280))
        clearButton.Bind(wx.EVT_BUTTON, self.OnClose)

        self.file_drop_target = DropTarget(self)
        self.SetDropTarget(self.file_drop_target)

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(panel1, 0, wx.EXPAND,0)
        box.Add(panel2, 0, wx.EXPAND,0)

        self.SetAutoLayout(True)
        self.SetSizer(box)
        self.Layout()

    def OnClose(self, e):
        self.Close(True)

app = wx.App()
frame = MyFrame(None, -1, "Sizer Test")
frame.Show()
app.MainLoop()

这可能不是你想要实现的,但至少它是一个起点,拖放工作。

© www.soinside.com 2019 - 2024. All rights reserved.