2017-10-20 113 views
0

首先,this就是我試圖用wxPython所做的。我希望標有「圖片1」的區域能夠接受拖動的圖像,然後當拖動圖像時,將其替換爲該圖像的縮略圖。我研究了wxPython中的「拖放」,但我似乎無法找到所需的工具來執行此操作。在wxPython中爲圖像創建拖放界面

任何幫助讓我開始正確的軌道將不勝感激。

回答

1

您將需要Pillow package來創建縮略圖。另一件你會想要的是最有可能的wx.FileDropTarget類。最後我做了以下內容:

import os 
import wx 

from PIL import Image 
from wx.lib.pubsub import pub 

PhotoMaxSize = 240 


class DropTarget(wx.FileDropTarget): 

    def __init__(self, widget): 
     wx.FileDropTarget.__init__(self) 
     self.widget = widget 

    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 


class PhotoCtrl(wx.App): 
    def __init__(self, redirect=False, filename=None): 
     wx.App.__init__(self, redirect, filename) 
     self.frame = wx.Frame(None, title='Photo Control') 

     self.panel = wx.Panel(self.frame) 
     pub.subscribe(self.update_image_on_dnd, 'dnd') 

     self.PhotoMaxSize = 240 

     self.createWidgets() 
     self.frame.Show() 

    def createWidgets(self): 
     instructions = 'Browse for an image' 
     img = wx.Image(240,240) 
     self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, 
             wx.Bitmap(img)) 
     filedroptarget = DropTarget(self) 
     self.imageCtrl.SetDropTarget(filedroptarget) 

     instructLbl = wx.StaticText(self.panel, label=instructions) 
     self.photoTxt = wx.TextCtrl(self.panel, size=(200,-1)) 
     browseBtn = wx.Button(self.panel, label='Browse') 
     browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse) 

     self.mainSizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer = wx.BoxSizer(wx.HORIZONTAL) 

     self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY), 
          0, wx.ALL|wx.EXPAND, 5) 
     self.mainSizer.Add(instructLbl, 0, wx.ALL, 5) 
     self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5) 
     self.sizer.Add(self.photoTxt, 0, wx.ALL, 5) 
     self.sizer.Add(browseBtn, 0, wx.ALL, 5) 
     self.mainSizer.Add(self.sizer, 0, wx.ALL, 5) 

     self.panel.SetSizer(self.mainSizer) 
     self.mainSizer.Fit(self.frame) 

     self.panel.Layout() 

    def onBrowse(self, event): 
     """ 
     Browse for file 
     """ 
     wildcard = "JPEG files (*.jpg)|*.jpg" 
     dialog = wx.FileDialog(None, "Choose a file", 
           wildcard=wildcard, 
           style=wx.OPEN) 
     if dialog.ShowModal() == wx.ID_OK: 
      self.photoTxt.SetValue(dialog.GetPath()) 
     dialog.Destroy() 
     self.onView() 

    def update_image_on_dnd(self, filepath): 
     self.onView(filepath=filepath) 

    def onView(self, filepath=None): 
     if not filepath: 
      filepath = self.photoTxt.GetValue() 

     img = wx.Image(filepath, wx.BITMAP_TYPE_ANY) 
     # scale the image, preserving the aspect ratio 
     W = img.GetWidth() 
     H = img.GetHeight() 
     if W > H: 
      NewW = self.PhotoMaxSize 
      NewH = self.PhotoMaxSize * H/W 
     else: 
      NewH = self.PhotoMaxSize 
      NewW = self.PhotoMaxSize * W/H 
     img = img.Scale(NewW,NewH) 

     self.imageCtrl.SetBitmap(wx.Bitmap(img)) 
     self.panel.Refresh() 

if __name__ == '__main__': 
    app = PhotoCtrl() 
    app.MainLoop() 

這與wxPython的爲我工作在Windows 7上。注意,我拖着從Windows資源管理器的文件到我的wxPython應用程序的形象窗口小部件。

+0

非常感謝!我發現的唯一問題是對BitmapFromImage的調用已被折舊。 (我相信我在Python 3.6.3中使用了最新版本的wxPython)。 –

+1

我以爲我得到了所有棄用的東西。我只是更新了代碼來修復這些警告。 –

+0

非常廣泛的答案,值得更多的信貸,然後它已經得到了迄今。 – Montmons