我想显示一个浮动在图像上的十字准线。下面是一段代码:
# -*- 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
十字准线出现了。