EVT_SIZE

Hello, I am trying to use the EVT_SIZE tool for resizing items to fit
windows, and am following the example in doodle.py. In the code below,
a small panel containing a dc is created, but when it is maximized only
the background is resized to fit the large window and the painted light
blue lines stay the original size. Am I invoking EVT_SIZE in the wrong
place?

Thanks,
Gabriel

···

############
import wx

class MyPanel(wx.Panel):
def init(self,parent,id):
wx.Panel.init(self,parent,id,size=(250,500))
size = self.GetClientSize()

class MyFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘Global Context’,size=(250,500))
self.panel = MyPanel(self,-1)
self.SetBackgroundColour(“Red”)
self.InitBuffer()
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

def InitBuffer(self):
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedDC(None, self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.DrawLines(dc)
    self.reInitBuffer = False
    wx.EVT_SIZE(self, self.OnSize)

def OnSize(self,event):
    self.reInitBuffer = True
   
def DrawLines(self, dc):
   
    dc.BeginDrawing()
   
    y = 1
    while y < 250:
        self.pen = wx.Pen('Light Blue', 3, wx.SOLID)
        dc.SetPen(self.pen)
        x = 0
        coords = []
        while x < 251:
            coords.append((x,y,x+1,y+1))
            x += 1
       
        for eachcoord in coords:
            dc.DrawLine(*eachcoord)
       
        y += 1
    dc.EndDrawing()
   
def OnPaint(self,event):
    dc = wx.BufferedPaintDC(self.panel, self.buffer)

app = wx.PySimpleApp(redirect=‘True’)

frm = MyFrame()
frm.SetSize((250,500))
frm.Show()

app.SetTopWindow(frm)
app.MainLoop()

Gabriel Murray wrote:

Hello, I am trying to use the EVT_SIZE tool for resizing items to fit windows, and am following the example in doodle.py. In the code below, a small panel containing a dc is created, but when it is maximized only the background is resized to fit the large window and the painted light blue lines stay the original size. Am I invoking EVT_SIZE in the wrong place?

Thanks,
Gabriel

############
import wx

class MyPanel(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self,parent,id,size=(250,500))
        size = self.GetClientSize()

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Global Context',size=(250,500))
        self.panel = MyPanel(self,-1)
        self.SetBackgroundColour("Red")
        self.InitBuffer()
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

You should bind the EVT_SIZE event here the same way you do the EVT_PAINT event. Doing it in InitBuffer means that a new binding will be done every time InitBuffer is called, possibly resulting in thousands (or more) items in the event table.

           def InitBuffer(self):
        size = self.GetClientSize()
        self.buffer = wx.EmptyBitmap(size.width, size.height)
        dc = wx.BufferedDC(None, self.buffer)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        self.DrawLines(dc)
        self.reInitBuffer = False
        wx.EVT_SIZE(self, self.OnSize)

    def OnSize(self,event):
        self.reInitBuffer = True

You are not doing anything that is using this reInitBuffer flag. If you'll look again at doodle you'll see that there is a EVT_IDLE handler that is checking the flag and then calling InitBuffer if it is True. The way you have it now the buffer is never resized to fill the new window size. You can also just call InitBuffer from here if you want to make it a little simpler. The doodle sample uses the flag and idle event to reduce the number of times that the buffer is recreated while resizing.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks for the help. I’ve made those changes. I still find that only
the background expands to fit the larger window. Is this because I am
using absolute coordinates (e.g. while x < 250) for doing the
painting rather than referring to the size of the buffer when I am
drawing? Or should it expand to the full size even if coding it as x
< 250.
Gabriel

···

On 7/17/06, Robin Dunn robin@alldunn.com wrote:

Gabriel Murray wrote:

Hello, I am trying to use the EVT_SIZE tool for resizing items to fit
windows, and am following the example in doodle.py. In the code below, a
small panel containing a dc is created, but when it is maximized only

the background is resized to fit the large window and the painted light
blue lines stay the original size. Am I invoking EVT_SIZE in the wrong
place?

Thanks,
Gabriel

############
import wx

class MyPanel(wx.Panel):
def init(self,parent,id):
wx.Panel.init(self,parent,id,size=(250,500))
size = self.GetClientSize
()

class MyFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘Global Context’,size=(250,500))
self.panel = MyPanel(self,-1)
self.SetBackgroundColour(“Red”)
self.InitBuffer()
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

You should bind the EVT_SIZE event here the same way you do the
EVT_PAINT event. Doing it in InitBuffer means that a new binding will

be done every time InitBuffer is called, possibly resulting in thousands
(or more) items in the event table.

def InitBuffer(self):
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedDC(None, self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.DrawLines(dc)
    self.reInitBuffer = False
    wx.EVT_SIZE(self, self.OnSize)

def OnSize(self,event):
    self.reInitBuffer = True

You are not doing anything that is using this reInitBuffer flag. If

you’ll look again at doodle you’ll see that there is a EVT_IDLE handler
that is checking the flag and then calling InitBuffer if it is True.
The way you have it now the buffer is never resized to fill the new

window size. You can also just call InitBuffer from here if you want to
make it a little simpler. The doodle sample uses the flag and idle
event to reduce the number of times that the buffer is recreated while

resizing.


Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

Gabriel Murray wrote:

Thanks for the help. I've made those changes. I still find that only the background expands to fit the larger window. Is this because I am using absolute coordinates (e.g. while x < 250) for doing the painting rather than referring to the size of the buffer when I am drawing?

Yes.

Or should it expand to the full size even if coding it as x < 250.

To do that you would need to set the scale of the DC.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!