ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> wxpython-users
wxpython-users
[wxpython-users] What's going on with wx.Pen()?
by Marlin Rowley other posts by this author
May 8 2008 2:32PM messages near this date
Re: [wxpython-users] wxGrid - no row labels | Re: [wxpython-users] 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 dynam
ically size it when the user drags the mouse.  This is going to be used for viewing only ima
ges within the window.  I want the pen to be bright red.  And I want it to be that way ALL t
he 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 (ba
ckground)
        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 or
iginal 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.
http://www.windowsvista.com/SP1?WT.mc_id=hotmailvistasp1banner
Thread:
Marlin Rowley
Tim Roberts
Marlin Rowley
Tim Roberts
Marlin Rowley
Christopher Barker
Marlin Rowley
Christopher Barker
Christopher Barker
Marlin Rowley

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved