[wxPython-users] draw rectangle on top of a picture with DC ?

Christopher Barker Chris.Barker at noaa.gov
Tue Feb 12 11:16:33 PST 2008


> In the code below, I manage to show up the buttons, but the picture does 
> not show (and the rectangle does not either).
> The picture is in the working directory
> 
> 
> Could someone tell me what I am doing wrong ?

There are a lot of odd things in this code -- it needs some re-structuring.

First, please post a png with the code, so that I don't have to go find 
my own to test it.

> What should I use wx .Paint, wx.Memory , ... ?

You need a Paint event handler.

What's happening is that you are drawing with the ClientDC in the 
method, before the Frame is Shown. then it is show, which causes a Paint 
event, which wipes out whatever you drew. Also it probably wasn't the 
right size when you drew it. These should help:

http://wiki.wxpython.org/RecipesImagesAndGraphics

Also be sure to read this:

http://wiki.wxpython.org/wxPython_Style_Guide

Notes on your code:

>     def partial_render(self,event):
>         self.Panel_1 = wx.Frame(None, wx.ID_ANY,style =  
> wx.SP_LIVE_UPDATE | wx.SP_3D ,size=(640, 360))

It's odd and confusing to call a Frame "Panel_1".

Also, you really want to create a custom wx.Frame (or wx.Dialog) class, 
and put the button creation, etc in the __init__.


>         self.dc = wx.ClientDC(self.Panel_1)
>         img=wx.Image('00.png').Scale(480, 320,wx.IMAGE_QUALITY_HIGH)
>         self.wxbmp=img.ConvertToBitmap()

you probably want this in the __init__, so you can store the bitmap.

>         self.dc.DrawBitmap(self.wxbmp, 20,20, True)
> 
>         self.dc.DrawRectangle(10, 15, 150, 200)

The drawing goes in a EVT_PAINT handler.

-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 at noaa.gov




More information about the wxpython-users mailing list