All:
I’m confused. I’ve set up a rectangular region program that will draw a rectangle and dynamically size it when the user drags the mouse. This is going to be used for viewing only images within the window. I want the pen to be bright red. And I want it to be that way ALL the time! For some reason, when I change the background color of my brush to something OTHER than black, I get a different color. Why?
Here’s the code in it’s entirety:
import wx
class RectWidget(wx.Window):
def init(self,parent,ID,size):
wx.Window.init(self,parent,ID)
# mouse selection start point
self.m_stpoint=wx.Point(0,0)
# mouse selection end point
self.m_endpoint=wx.Point(0,0)
# mouse selection cache point
self.m_savepoint=wx.Point(0,0)
self.SetClientSize(size)
self.InitBuffer()
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def InitBuffer(self):
size = self.GetClientSize()
self.buffer = wx.EmptyBitmap(size.width, size.height)
dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)
dc.SetBackground(wx.Brush("Black")) <<<<<<<<<<<<<<<<<<< ORIGINAL BRUSH COLOR (background)
dc.Clear()
def OnLeftDown(self, event):
# Left mouse button down, change cursor to
# something else to denote event capture
self.m_stpoint = event.GetPosition()
cur = wx.StockCursor(wx.CURSOR_CROSS)
self.SetCursor(cur)
# invalidate current canvas
self.InitBuffer()
self.Refresh()
# cache current position
self.m_savepoint = self.m_stpoint
self.CaptureMouse()
def OnMotion(self,event):
if event.Dragging() and event.LeftIsDown():
# get device context of canvas
dc= wx.BufferedDC(wx.ClientDC(self),self.buffer)
# Set logical function to XOR for rubberbanding
dc.SetLogicalFunction(wx.XOR)
# Set dc brush and pen
# Here I set brush and pen to white and grey respectively
# You can set it to your own choices
# The brush setting is not really needed since we
# dont do any filling of the dc. It is set just for
# the sake of completion.
wbrush = wx.Brush("White", wx.TRANSPARENT)
wpen = wx.Pen("Red", 1, wx.SOLID) <<< NOT WORKING WHEN original Brush is something other than black!
dc.SetBrush(wbrush)
dc.SetPen(wpen)
self.drawMotion(dc,event)
def drawMotion(self,dc,event):
# reset dc bounding box
dc.ResetBoundingBox()
dc.BeginDrawing()
w = (self.m_savepoint.x - self.m_stpoint.x)
h = (self.m_savepoint.y - self.m_stpoint.y)
# To erase previous rectangle
dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h)
# Draw new rectangle
self.m_endpoint = event.GetPosition()
w = (self.m_endpoint.x - self.m_stpoint.x)
h = (self.m_endpoint.y - self.m_stpoint.y)
# Set clipping region to rectangle corners
dc.SetClippingRegion(self.m_stpoint.x, self.m_stpoint.y, w,h)
dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h)
dc.EndDrawing()
self.m_savepoint = self.m_endpoint # cache current endpoint
def OnLeftUp(self,event):
if self.HasCapture():
# User released left button, change cursor back
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
self.ReleaseMouse()
def OnPaint(self,event):
dc = wx.BufferedPaintDC(self,self.buffer)
class ToolbarFrame(wx.Frame):
def init(self,parent,id):
wx.Frame.init(self,parent,id, ‘Render Region’, size=(640,480))
statusBar = self.CreateStatusBar()
self.region = RectWidget(self,-1,size=(640,480))
if name == ‘main’:
app = wx.PySimpleApp()
frame = ToolbarFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()