first, a couple style comments:
- it really is better to use spaces than tabs to indent -- it's more or less the python standard.
- ID_TOOL_BRUSH=wx.NewId()
ID_TOOL_LINE=wx.NewId()
Explicit IDs are kind of ugly, see:
http://wiki.wxpython.org/wxPython_Style_Guide
Instead of ID_TOOL_whatever, you could have a tool object, or a string, or better yet, put your drawing code, etc into a tool object, then you wouldn't even need to know what kind of tool object it was.
But now to the real issues:
Your Paint handler should call the same Draw method -- rather than duplicating the code there.
One good method for doing this kind of thing is to not actually re-draw the whole buffer each time. what you can do instead is draw a "shadow" of your object as the mouse moves -- you do this with a ClientDC and set the DC mode to XOR -- draw once, you get a reverse color version, draw again and it's back to the original bitmap underneath.
However, what you've doing should work OK -- drawing bitmaps to the screen is very fast. a few issue though:
dc=wx.BufferedDC(wx.ClientDC(self), self.buffer)
dc.DrawBitmap(self.buffer, 0, 0)
you don't want to draw the buffer to a buffered DC, it will draw itself. You want a regular ClientDC. And you want to just draw there, rather than in a Paint Handler (you could do it there, but you're kind of doing both now)
Then, on mouse-up, you need to draw the final line to the buffer -- note that this is why you want your drawing code to be in a Line object -- then you can put it in only one place.
By the way, I'd like to discourage you from making a bitmap editor like this -- why not make it a vector object editor -- you always save the resulting bitmap out later, but if you keep vector objects, then you can change them, move them, re-scale, etc, etc.
You might want to look at wx.lib.floatcanvas for some ideas. Or the latest version and extra demos here:
http://morticia.cs.dal.ca/FloatCanvas/
Enclosed is my edited version, which seems to work. It could still use some cleaning up.
junk.py (4.2 KB)
···
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov