【问题标题】:wxpython move drawn point with mousewxpython用鼠标移动绘制点
【发布时间】:2015-08-28 02:53:50
【问题描述】:

我现在正在使用 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()

【问题讨论】:

    标签: python user-interface wxpython


    【解决方案1】:

    您不想绘制一个新圆,而是通过更新参数列表来移动现有圆。

    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()
    

    【讨论】:

    • 感谢您的回答。是的,我想移动现有的面点。您的答案是使用计时器事件并在框架上绘制。当我使用鼠标移动事件并在 StaticBitmap 上绘图时可以吗?
    • 看在上帝的份上。这是一个例子!
    【解决方案2】:

    我用脏方法实现了这个效果。但我不知道它是否是属性方法。如果方法错误,请告诉我,我将更新或删除此答案。这是我的代码:

    # -*- 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()
    

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多