What's going on with wx.Pen()?

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()

···

Make Windows Vista more reliable and secure with Windows Vista Service Pack 1. Learn more.

Marlin Rowley wrote:

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?

Is it really that confusing? Notice:

    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)

That's the reason. You are drawing with a red pen, but the logical function means that the color of the pen is XOR-ed with the color of the background. When the background is black, the pixel value is 0, and anything XORed with 0 is itself. When the background is not black, XORing with red simple inverts the red component in whatever color was there before.

This is absolutely normal behavior for rubber banding. If you really want a red rubber band all of the time, then you can't use a simple erase-the-old and draw-the-new scheme for drawing the rubber band. You will have to use some kind of double-buffered scheme, by keeping a copy of the background in a bitmap. Then every time the rubber banding changes, you'll have to copy the virgin bitmap to the screen, then draw the rubber band on top of it.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Marlin Rowley wrote:

event.Dragging() and even.LeftIsDown():
   # Refresh the background with the bitmap
   # draw a solid rectangle

yup.

-Chris

···

--
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