2013-06-21 26 views
0

我試圖在wxpython程序中實現平移功能。該程序由一個框架組成,該框架包含一個保存位圖圖像的ScrolledWindow。wxpython泛執行問題

在空白麪板上單擊並平移會產生預期行爲。點擊並平移圖像會產生約10px的「跳躍」。

如果在拖動過程中未更新mouse_pos成員,則平移圖像平滑但在空白麪板上平移是錯誤的。

系統信息:wxpython 2.8.12.1,Python 2.7.3 64bit和Windows 7 64bit。

import wx 
import inspect 
import threading 

class MyFrame(wx.Frame): 
    def __init__(self, parent, mytitle): 
     wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=(350, 350)) 

     self.scrollwin = wx.ScrolledWindow(self, wx.ID_ANY) 
     width = 1000 
     height = 1000 
     self.scrollwin.SetScrollbars(20, 20, width/20, height/20) 
     self.scrollwin.SetScrollRate(1, 1) 
     self.scrollwin.Bind(wx.EVT_MOTION, self.on_mouse_drag) 
     self.scrollwin.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) 
     self.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) 

     image_file = "test.jpg" 
     image = wx.Bitmap(image_file) 
     sb = wx.StaticBitmap(self.scrollwin, wx.ID_ANY, image) 
     sb.Bind(wx.EVT_MOTION, self.event_defer) 
     sb.Bind(wx.EVT_LEFT_DOWN, self.event_defer) 
     self._mouse_pos = (0, 0) 
     self.sem = threading.Semaphore(0) 

    @property 
    def mouse_pos(self): 
     return self._mouse_pos 

    @mouse_pos.setter 
    def mouse_pos(self, value): 
     print "mouse_pos:" 
     print "Calling from: ", inspect.stack()[1][3] 
     print value 
     self._mouse_pos = value 

    def on_mouse_drag(self, event): 
     if event.Dragging() and event.LeftIsDown(): 
      self.sem.release() 
      print self.sem._Semaphore__value 
      (mouse_x, mouse_y) = self.mouse_pos 

      #new_pos = self.scrollwin.CalcUnscrolledPosition(event.GetPosition()).Get() 
      #new_pos = self.scrollwin.CalcScrolledPosition(event.GetPosition()).Get() 
      new_pos = event.GetPositionTuple() 
      (new_x, new_y) = new_pos 

      scrollpos = self.scrollwin.GetViewStart() 
      (scroll_x, scroll_y) = scrollpos 

      (delta_x, delta_y) = ((new_x - mouse_x), (new_y - mouse_y)) 

      (scroll_x, scroll_y) = ((scroll_x - delta_x), (scroll_y - delta_y)) 
      print "Scroll:" 
      print (scroll_x, scroll_y) 
      self.scrollwin.Scroll(scroll_x, scroll_y) 

      #self.mouse_pos = self.scrollwin.CalcUnscrolledPosition(event.GetPosition()).Get() 
      #self.mouse_pos = self.scrollwin.CalcScrolledPosition(event.GetPosition()).Get() 
      self.mouse_pos = new_pos # Disabling this gives smooth panning on the image 

      self.sem.acquire(False) 

    def on_left_down(self, event): 
     #self.mouse_pos = self.scrollwin.CalcUnscrolledPosition(event.GetPosition()).Get() 
     #self.mouse_pos = self.scrollwin.CalcScrolledPosition(event.GetPosition()).Get() 
     self.mouse_pos = event.GetPositionTuple() 

    def event_defer(self, event): 
     event.ResumePropagation(1) 
     event.Skip() 

app = wx.App() 
MyFrame(None, "Test wx.ScrolledWindow()").Show() 
app.MainLoop() 

回答

1

我發現這是偶然,但我試過運行它,它看起來不錯,當我平移圖像時沒有跳躍。我在Ubuntu 12.04(64位)上運行python 2.7.3,所以也許Windows有一個問題,但它在Linux上的確很好。

+0

這是輸入。我懷疑這樣的事情。最後,我使用了來自wx.lib.dragscroller.DragScroller的內置庫小部件DragScroller。 – dilbert