2013-09-22 23 views
2

我想顯示浮動在圖像上的十字線。以下是代碼:如何在wxpython中顯示wx.DC的CrossHair?

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

import wx 

class Locator(wx.Frame): 
    def __init__(self, title, size, style): 
     super(Locator, self).__init__(parent = None, id = -1, 
             title = title, size = size, style = style) 

     self.panel = wx.Panel(self) 

     self.menu = wx.MenuBar() 
     self.SetMenuBar(self.menu) 

     self.vbox = wx.BoxSizer(wx.VERTICAL) 
     self.imgbox = wx.BoxSizer(wx.HORIZONTAL) 
     self.img = wx.Image('test.jpg') 
     self.imgbmp = wx.StaticBitmap(self.panel, 
             bitmap = wx.BitmapFromImage(self.img), 
             size = (1325, 614)) 

     self.panel.SetSizer(self.vbox) 
     self.vbox.Add(self.imgbox, flag = wx.ALIGN_CENTER) 
     self.imgbox.Add(self.imgbmp) 
     self.imgbmp.Bind(wx.EVT_MOTION, self.OnMouseMove) 

     self.Show() 

    def OnMouseMove(self, e): 
     (x, y) = e.GetPosition() 
     dc = wx.ClientDC(self.imgbmp) # probelm here! 
     dc.Clear() 
     dc.SetPen(wx.Pen(wx.Color(0, 0, 0), 1, wx.DOT)) 
     dc.CrossHair(x, y) 

if __name__ == '__main__': 
    app = wx.App() 
    Locator('Locator', 
       size = (1350, 700), 
       style = wx.DEFAULT_FRAME_STYLE^wx.RESIZE_BORDER^wx.MAXIMIZE_BOX) 
    app.MainLoop() 

的問題是,我不知道哪個對象將是wx.ClientDC參數。當我給self作爲參數時,test.jpg正確顯示,但沒有十字線。 self.imgbox作爲參數,出現錯誤:

Traceback (most recent call last): 
     File "tmp.py", line 50, in OnMouseMove 
     dc = wx.ClientDC(self.imgbox) 
     File "C:\Users\songsong\AppData\Local\Enthought\Canopy\User\lib\site-packages\wx\_gdi.py", line 4774, in __init__ 
     _gdi_.ClientDC_swiginit(self,_gdi_.new_ClientDC(*args, **kwargs)) 
    TypeError: in method 'new_ClientDC', expected argument 1 of type 'wxWindow *' 

而且self.imgbmp的說法,沒有test.jpg但十字線出現了。

回答

1

我花了一段時間才重新獲得了對wxPython的熟練程度,但是在這裏我們走了。該方法非常簡單,但您不應該一次嘗試完成兩個操作 - 獲取鼠標位置並繪製十字準線。

  1. 首先,獲取鼠標位置並保存。然後觸發重繪。
  2. 二,聽重繪事件,獲得一個DC。
    然後繼續繪製圖像,然後將十字準線放在上面。

我縮短你的示例代碼,以使你更容易被發現的最重要的變化:

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

import wx 

class Locator(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     wx.Frame.__init__(self, *args, **kwargs) 
     vbox = wx.BoxSizer(wx.VERTICAL) 
     self.SetSizer(vbox) 
     self.panel = MyPanel(self) 
     vbox.Add(self.panel, 2, wx.EXPAND) 
     self.Show() 

class MyPanel(wx.Panel): 
    def __init__(self, *args, **kwargs): 
     wx.Panel.__init__(self, *args, **kwargs) 
     self.img = wx.Image('test.jpg') 
     self.bmp = wx.BitmapFromImage(self.img) 
     self.Bind(wx.EVT_MOTION, self.OnMouseMove) 
     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.coordinates = (0, 0) 

    def OnMouseMove(self, e): 
     self.coordinates = e.GetPosition() 
     self.Refresh() 

    def OnPaint(self, e): 
     dc = wx.PaintDC(self) 
     dc.DrawBitmap(self.bmp, 0, 0, False) 
     dc.SetPen(wx.Pen(wx.Color(0, 0, 0), 1, wx.DOT)) 
     dc.CrossHair(self.coordinates[0], self.coordinates[1]) 

if __name__ == '__main__': 
    app = wx.App() 
    Locator(None, -1, title='Locator') 
    app.MainLoop() 
+0

我檢查你的答案晚了,對不起。無論如何,我會嘗試。 – songsong