draw an image in wx.BufferedDC onto the page created by AddPage of wx.Notebook

zxo102 zxo102 at gmail.com
Thu Aug 10 11:12:13 EDT 2006


Hi everyone,
   I have tried two days to figure out how to draw the image in
wx.BufferedDC on the page created by AddPage of wx.Notebook but still
got no clue.

   The attached example works fine. If I click the menu "Draw" --> "New
Drawing". The image with wx.BufferedDC/wx.BufferedPaintDC can move
around on the frame. But If I uncomment those three commented lines in
"class TestFrame" to add a new page (from wx.Notebook) with a tag and
modify the last line like
        self.Window = DrawWindow(form2)
I can not see the image from wx.BufferedDC anywhere and don't know what
is going on.

 I need your help. Thanks a lot.


The attached example is based on the example on the site:
   http://wiki.wxpython.org/index.cgi/DoubleBufferedDrawing

ouyang


import wx
import random
import os,time

USE_BUFFERED_DC = 1

def opj(path):
    """Convert paths to the platform-specific separator"""
    str = apply(os.path.join, tuple(path.split('/')))
    # HACK: on Linux, a leading / gets lost...
    if path.startswith('/'):
        str = '/' + str
    return str


class BufferedWindow(wx.Window):
    def __init__(self, parent, id,
                 pos = wx.DefaultPosition,
                 size = wx.DefaultSize,
                 style=wx.NO_FULL_REPAINT_ON_RESIZE):
        wx.Window.__init__(self, parent, id, pos, size, style)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_SIZE, self.OnSize)

        self.bmp0 = wx.Image(opj('image.bmp'), wx.BITMAP_TYPE_BMP)
        self.bmp0.SetMask(True)
        self.bmp = self.bmp0.Rotate(1.1215, (50,10), True,None)
        self.bmp = self.bmp.ConvertToBitmap()
	self.x = 100
	self.y = 100
        self.angle = 0.0
	self.OnSize(None)

    def Draw(self,dc):
        pass

    def OnPaint(self, event):
        if USE_BUFFERED_DC:
            dc = wx.BufferedPaintDC(self, self._Buffer)
        else:
            dc = wx.PaintDC(self)
            dc.DrawBitmap(self._Buffer,0,0)

    def OnSize(self,event):
        self.Width, self.Height = self.GetClientSizeTuple()
        self._Buffer = wx.EmptyBitmap(self.Width, self.Height)
        self.UpdateDrawing()

    def SaveToFile(self,FileName,FileType):
        self._Buffer.SaveFile(FileName,FileType)

    def UpdateDrawing(self):
        if USE_BUFFERED_DC:
             dc = wx.BufferedDC(wx.ClientDC(self), self._Buffer)
             self.Draw(dc)
        else:
            # update the buffer
            dc = wx.MemoryDC()
            dc.SelectObject(self._Buffer)

            self.Draw(dc)
            # update the screen
            wx.ClientDC(self).Blit(0, 0, self.Width, self.Height, dc,
0, 0)

class DrawWindow(BufferedWindow):
    def __init__(self, parent, id = -1):
        BufferedWindow.__init__(self, parent, id)

    def Draw(self, dc):
        dc.BeginDrawing()
        dc.SetBackground( wx.Brush("White") )
        dc.Clear() # make sure you clear the bitmap!
        bmp = self.bmp0.Rotate(self.angle, (self.x,self.y), True,None)
        bmp = bmp.ConvertToBitmap()
        dc.DrawBitmap(bmp, self.x,self.y, True)
        dc.EndDrawing()

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Double Buffered Test",
                         wx.DefaultPosition,
                         size=(500,500),
                         style=wx.DEFAULT_FRAME_STYLE |
wx.NO_FULL_REPAINT_ON_RESIZE)

        MenuBar = wx.MenuBar()
        file_menu = wx.Menu()
        ID_EXIT_MENU = wx.NewId()
        file_menu.Append(ID_EXIT_MENU, "E&xit","Terminate the program")
        self.Bind(wx.EVT_MENU, self.OnQuit, id=ID_EXIT_MENU)
        MenuBar.Append(file_menu, "&File")

        draw_menu = wx.Menu()
        ID_DRAW_MENU = wx.NewId()
        draw_menu.Append(ID_DRAW_MENU, "&New Drawing","Update the
Drawing Data")
        self.Bind(wx.EVT_MENU, self.NewDrawing, id=ID_DRAW_MENU)
        BMP_ID = wx.NewId()
        draw_menu.Append(BMP_ID,'&Save Drawing\tAlt-I','')
        self.Bind(wx.EVT_MENU, self.SaveToFile, id=BMP_ID)
        MenuBar.Append(draw_menu, "&Draw")

        self.SetMenuBar(MenuBar)

	#nb = wx.Notebook(self, -1)
        #form2 = Form2(nb, -1)
        #nb.AddPage(form2, "Sizers")

	self.Window = DrawWindow(self)

    def OnQuit(self,event):
        self.Close(True)

    def NewDrawing(self,event):
	if event==None:
             self.Window.UpdateDrawing()
        else:
	    self.Window.y = 100
	    for i in range(10):
	       time.sleep(1)
               self.Window.y = self.Window.y + 5
	       self.Window.angle = self.Window.angle+0.1*1
               self.Window.UpdateDrawing()

    def SaveToFile(self,event):
        dlg = wx.FileDialog(self, "Choose a file name to save the image
as a PNG to",
                           defaultDir = "",
                           defaultFile = "",
                           wildcard = "*.png",
                           style=wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            self.Window.SaveToFile(dlg.GetPath(),wx.BITMAP_TYPE_PNG)
        dlg.Destroy()

class Form2(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, -1)

class DemoApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers() # called so a PNG can be saved
        frame = TestFrame()
        frame.Show(True)

        frame.NewDrawing(None)

        self.SetTopWindow(frame)

        return True

if __name__ == "__main__":
    app = DemoApp(0)
    app.MainLoop()




More information about the Python-list mailing list