#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- """ 描画テスト。 マウスボタンを押した時だけ画面に何かを描画する。 ただし、この書き方では画面がちらつく。 VerySimpleDrawing - wxPyWiki https://wiki.wxpython.org/VerySimpleDrawing wxPythonのPaintDCでアンチエイリアスや透過を使う :右京web http://hujimi.seesaa.net/article/161079355.html """ import wx bg_image = "images/bg.jpg" class DrawPanel(wx.Frame): def __init__(self, *args, **kwargs): """初期化""" wx.Frame.__init__(self, *args, **kwargs) # ワーク確保 self.mouseLeftFlag = False self.pos = wx.Point(0, 0) # イベント割り当て self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp) self.Bind(wx.EVT_MOTION, self.OnMouseMove) # 背景bitmap画像読み込み image = wx.Image(bg_image) self.bitmap = image.ConvertToBitmap() # 画像サイズでフレームサイズを設定し直し self.SetSize(image.GetSize()) def OnMouseLeftDown(self, e): """ 左ボタンを押した時の処理 """ pos = e.GetPosition() self.pos = pos self.mouseLeftFlag = True self.Refresh() def OnMouseMove(self, e): """ マウスカーソルを動かした時の処理 """ if self.mouseLeftFlag: pos = e.GetPosition() self.pos = pos self.Refresh() def OnMouseLeftUp(self, e): """ 左ボタンを離した時の処理 """ pos = e.GetPosition() self.pos = pos self.mouseLeftFlag = False self.Refresh() def OnPaint(self, event=None): """ 描画処理 """ pdc = wx.PaintDC(self) try: # アンチエイリアスをかける dc = wx.GCDC(pdc) except: dc = pdc dc.Clear() dc.DrawBitmap(self.bitmap, 0, 0, True) # 背景ビットマップ画像を描画 if self.mouseLeftFlag: # 円を描画 dc.SetBrush(wx.Brush(wx.Colour(255, 0, 0, 64))) dc.SetPen(wx.Pen("RED", 2)) x = self.pos.x - 50 y = self.pos.y - 50 dc.DrawEllipse(x, y, 100, 100) if __name__ == '__main__': app = wx.App(False) frame = DrawPanel(parent=None, title=u"ウインドウ内でマウスボタンを押してみるべし") frame.Show() app.MainLoop()