#!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 wxPython の画面に色々描く(+マウスのイベントを処理する) - 見切り発車 http://d.hatena.ne.jp/uyamae/20090305/1236261541 """ import wx 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) # 画像読み込み image = wx.Image("images/bg.jpg") self.bitmap = image.ConvertToBitmap() # 画像サイズでフレームサイズを設定し直し self.SetSize(image.GetSize()) def OnMouseLeftDown(self, e): """ 左ボタンを押した時の処理 """ pos = e.GetPosition() self.pos = pos self.mouseLeftFlag = True self.Refresh(False) def OnMouseMove(self, e): """ マウスカーソルを動かした時の処理 """ if self.mouseLeftFlag: pos = e.GetPosition() self.pos = pos self.Refresh(False) def OnMouseLeftUp(self, e): """ 左ボタンを離した時の処理 """ pos = e.GetPosition() self.pos = pos self.mouseLeftFlag = False self.Refresh(False) def OnPaint(self, event=None): """ 描画処理 """ pdc = wx.BufferedDC(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()