2015-08-28 68 views
1

我正在使用wxpython來做一些圖像處理工作。我想用鼠標左鍵畫出一個點,並用鼠標移動來移動繪製的點。wxpython用鼠標移動繪製點

以下是我的代碼。當左鍵點擊時畫點沒問題,但是當我移動鼠標時,先前繪製的點也顯示出不符合我的期望的點。

如何隱藏前一點當我移動鼠標,使它看起來像我移動點?

# -*- coding: utf-8 -*- 

import wx 

class MyFrame(wx.Frame): 
    isLeftDown = False 

    def __init__(self, parent): 
     wx.Frame.__init__(self, parent, id=wx.ID_ANY, size=wx.Size(500, 500)) 
     bSizer1 = wx.BoxSizer(wx.VERTICAL) 

     self.m_panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, 
           wx.DefaultSize, wx.TAB_TRAVERSAL) 
     bSizer1.Add(self.m_panel, 3, wx.EXPAND | wx.ALL, 5) 

     bmp = wx.EmptyBitmap(500, 500) 
     self.staticBMP = wx.StaticBitmap(self.m_panel, wx.ID_ANY, bmp) 

     self.SetSizer(bSizer1) 

     # bind event 
     self.staticBMP.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 
     self.staticBMP.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 
     self.staticBMP.Bind(wx.EVT_MOTION, self.OnMove) 

    def OnLeftDown(self, event): 
     pos = event.GetPositionTuple() 
     dc = wx.ClientDC(self.staticBMP) 
     dc.DrawCircle(pos[0], pos[1], 5) 
     self.isLeftDown = True 

    def OnLeftUp(self, event): 
     self.isLeftDown = False 

    def OnMove(self, event): 
     if self.isLeftDown: 
      pos = event.GetPositionTuple() 
      dc = wx.ClientDC(self.staticBMP) 
      dc.DrawCircle(pos[0], pos[1], 3) 


if __name__ == '__main__': 
    app = wx.App() 
    frame = MyFrame(None) 
    frame.Show(True) 
    app.MainLoop() 

回答

0

您不想繪製新的圓,而是通過更新參數列表來移動現有的圓。

import wx 

WHITE_COLOR = (255,255,255) 

class MoveCircle(): 

    def __init__(self, parent): 
     self.parent=parent 
     self.parameters = [36,36,30] 
     self.advance=3 
     self.parent.Bind(wx.EVT_PAINT, self.on_paint) 
     self.timer = wx.Timer(self.parent) 
     self.parent.Bind(wx.EVT_TIMER, self.on_timer, self.timer) 
     self.timer.Start(100) 

    def on_paint(self, event=None): 
     dc = wx.PaintDC(self.parent) 
     dc.SetBrush(wx.Brush("blue")) 
     dc.DrawCircle(*self.parameters) 

    def on_timer(self, event): 
     self.parameters[0] += self.advance 
     print self.parameters 
     if self.parameters[0] < 36 or self.parameters[0] > 210: 
      self.advance *= -1 
     self.parent.Refresh() 


if __name__ == "__main__": 
    app = wx.App() 
    title = "Circle" 
    frame = wx.Frame(None, wx.ID_ANY, title, size=(250, 200)) 
    MoveCircle(frame) 
    frame.Show(True) 
    app.MainLoop() 
+0

謝謝你的回答。是的,我想要移動現有的點。您的答案是使用計時器事件並在框架上繪圖。當我使用鼠標移動事件並在StaticBitmap上繪圖時,可以嗎? – tidy

+0

爲了上天的緣故。這是一個例子! –

0

我已經用髒方法達到了這個效果。但我不知道它是不是財產法。如果這是錯誤的方式,請告訴我,我會更新或刪除此答案。這裏是我的代碼:

# -*- coding: utf-8 -*- 

import wx 

class MyFrame(wx.Frame): 
    isLeftDown = False 

    def __init__(self, parent): 
     wx.Frame.__init__(self, parent, id=wx.ID_ANY, size=wx.Size(500, 500)) 
     bSizer1 = wx.BoxSizer(wx.VERTICAL) 

     self.m_panel = wx.Panel(self, wx.ID_ANY) 
     bSizer1.Add(self.m_panel, 3, wx.EXPAND | wx.ALL, 5) 

     self.bmp = wx.EmptyBitmap(500, 500) 
     self.staticBMP = wx.StaticBitmap(self.m_panel, wx.ID_ANY, self.bmp) 

     self.SetSizer(bSizer1) 

     # bind event 
     self.staticBMP.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 
     self.staticBMP.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 
     self.staticBMP.Bind(wx.EVT_MOTION, self.OnMove) 

    def OnLeftDown(self, event): 
     pos = event.GetPositionTuple() 
     dc = wx.ClientDC(self.staticBMP) 
     dc.DrawCircle(pos[0], pos[1], 5) 
     self.isLeftDown = True 

    def OnLeftUp(self, event): 
     self.isLeftDown = False 

    def OnMove(self, event): 
     if self.isLeftDown: 
      pos = event.GetPositionTuple() 
      dc = wx.ClientDC(self.staticBMP) 
      dc.DrawBitmap(self.bmp, 0, 0) 
      dc.DrawCircle(pos[0], pos[1], 5) 


if __name__ == '__main__': 
    app = wx.App() 
    frame = MyFrame(None) 
    frame.Show(True) 
    app.MainLoop()