2014-02-14 88 views
1

我想用拖放圖像編寫腳本。我上傳了2張圖片(.jpg)並嘗試在面板上拖動圖片。 當我在面板上拖動firstImage時 - 一切看起來都不錯。 但是當我在面板上拖動secondImage(特別是當secondImage與firstImage聯繫時)發生了一個錯誤。位於firstImage內容下的secondImage的內容。我怎麼做,當我拖動secondImage,其內容位於firstImage的內容?謝謝。使用wx.EVT_MOVE拖放圖像

enter image description here

script.py

import wx 
import wx.lib.buttons as buttons 

class Main(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__(self, parent, id=-1, title=title, size=(300, 300)) 

     self.panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, size=(300, 300)) 

     firstImage = ButtonImage(self, self.panel, r'.\image_wtf\1.jpg', (20,20)) 
     secondImage = ButtonImage(self, self.panel, r'.\image_wtf\2.jpg', (20, 135)) 

     self.Layout() 

class ButtonImage(): 
    def __init__(self, parent, panel, nameImage, pos): 
     self.panel = panel 

     self.bmp = wx.Bitmap(nameImage, wx.BITMAP_TYPE_ANY) 

     ### search for a piece of the maximum position (limit boundary) 
     self.maxPiecePositionX = self.panel.GetSize()[0] - self.bmp.GetSize()[0] 
     self.maxPiecePositionY = self.panel.GetSize()[1] - self.bmp.GetSize()[1] 

     self.bmapBtn = wx.BitmapButton(self.panel, id=wx.ID_ANY, bitmap=self.bmp, style=wx.NO_BORDER, pos=pos) 

     #self.bmapBtn.Bind(wx.EVT_ENTER_WINDOW, self.EnterButton, self.bmapBtn) 
     #self.bmapBtn.Bind(wx.EVT_LEAVE_WINDOW, self.LeaveButton, self.bmapBtn) 

     self.bmapBtn.Bind(wx.EVT_LEFT_DOWN, self.OnClickDown, self.bmapBtn) 
     self.bmapBtn.Bind(wx.EVT_LEFT_UP, self.OnClickUp, self.bmapBtn) 
     self.bmapBtn.Bind(wx.EVT_MOTION, self.MoveButton, self.bmapBtn) 

     #self.bmapBtn.Bind(wx.EVT_MOVE, self.MoveButtonTest, self.bmapBtn) 

     self.hold = 0 
     self.holdPosition = (0, 0) 

    def EnterButton(self, event): 
     pass 

    def LeaveButton(self, event): 
     self.hold = 0 

    def OnClickDown(self, event): 
     obj = event.GetEventObject() 
     self.hold = 1 
     self.holdPosition = (event.GetX(), event.GetY()) 

    def OnClickUp(self, event): 
     self.hold = 0 

    def MoveButton(self, event): 
     deltaX, deltaY = 0, 0 

     if self.hold: 
      deltaX = event.GetPosition()[0] - self.holdPosition[0] 
      deltaY = event.GetPosition()[1] - self.holdPosition[1] 

      newPositionX = self.bmapBtn.GetPosition()[0] + deltaX 
      newPositionY = self.bmapBtn.GetPosition()[1] + deltaY 

      if (0 < newPositionX < self.maxPiecePositionX) and (0 < newPositionY < self.maxPiecePositionY): 
       self.bmapBtn.SetPosition((newPositionX, newPositionY)) 
      else: 
       self.holdPosition = self.holdPosition[0] + deltaX, self.holdPosition[1] + deltaY 
      self.bmapBtn.Refresh() 

app = wx.PySimpleApp() 
frame = Main(None, u"Game") 
frame.Show() 
app.MainLoop() 

回答

1

你可以使用加註()對提高窗口到窗口層次結構的頂部。

def MoveButton(): 
    ................. 
    ................. 
    self.bmapBtn.Raise() 
    self.bmapBtn.Refresh()