Sizers again

self.GetContainingSizer().SetItemMinSize(self,(0,(dc.GetTextExtent(lines[0])[1]+verticalspacing)*lines.__len__()))
in DisplayText is supposed to make is so the sizer will be big enough for
the text, but it seems like it's always 1 refresh behind.
If you run the code and restore and maximize it, the DisplayText is the size
it should have been when you restored it.

import wx

info={'Country 1':{'comparison 1':[True,'text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text'],'comparison
2':[True,'text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text']},'Country 2':{'comparison 1':[True,'text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text'],'comparison 2':[True,'text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text']},'Country
3':{'comparison 1':[True,'text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text'],'comparison 2':[True,'text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text']}}

class frame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self,None,-1,'Comparing Countries')
    self.SetBackgroundColour('white')

    self.countries=info.keys()
    self.countries.sort()

    self.framesizer=wx.BoxSizer(wx.HORIZONTAL)

    #countries lists
    self.listsplitter=wx.SplitterWindow(self)
    self.listsplitter.SetSashGravity(.5)

    self.list1=wx.ListBox(self.listsplitter,choices=self.countries)
    self.list1.SetSelection(0)
    self.list2=wx.ListBox(self.listsplitter,choices=self.countries)
    self.list2.SetSelection(1)

    self.listsplitter.SplitHorizontally(self.list1,self.list2)

self.framesizer.Add(self.listsplitter,0,wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM,5)

    self.Bind(wx.EVT_LISTBOX,self.newcountry)
    #countries lists

    self.countryscroll=wx.ScrolledWindow(self)
    self.countrysizer=wx.BoxSizer(wx.VERTICAL)
    self.countryscroll.SetSizer(self.countrysizer)
    self.countryscroll.SetScrollbars(20,20,50,50)
    self.countryscroll.SetFocus()

    self.framesizer.Add(self.countryscroll,1,wx.EXPAND)
    self.SetMinSize((800,500))

    self.newcountry(None)
    self.SetSizer(self.framesizer)
    self.Maximize()
    self.Show()

  def newcountry(self,event):
    self.countrysizer.Clear(True)

    #country info
    titlessizer=wx.BoxSizer(wx.HORIZONTAL)
  
self.title1=wx.StaticText(self.countryscroll,label=self.list1.GetStringSelection(),style=wx.ALIGN_CENTER)
  
self.title2=wx.StaticText(self.countryscroll,label=self.list2.GetStringSelection(),style=wx.ALIGN_CENTER)
    self.title1.SetFont(wx.Font(40,wx.SWISS,wx.NORMAL,wx.NORMAL))
    self.title2.SetFont(wx.Font(40,wx.SWISS,wx.NORMAL,wx.NORMAL))
    titlessizer.Add(self.title1,1,wx.LEFT|wx.RIGHT,20)
    titlessizer.Add(self.title2,1,wx.LEFT|wx.RIGHT,20)

    self.countrysizer.Add(titlessizer,0,wx.EXPAND|wx.ALL,5)
    #country info

    keys=info[self.list1.GetStringSelection()].keys()
    keys.sort()
    for i in range(0,keys.__len__()):
      if
info[self.list1.GetStringSelection()][keys[i]][0]==info[self.list2.GetStringSelection()][keys[i]][0]:
      
temp=wx.StaticText(self.countryscroll,label=keys[i],style=wx.ALIGN_CENTER)
        temp.SetFont(wx.Font(30,wx.SWISS,wx.NORMAL,wx.NORMAL))
        self.countrysizer.Add(temp,0,wx.EXPAND|wx.ALL,5)

        tempsizer=wx.BoxSizer(wx.HORIZONTAL)
      
temp=DisplayText(self.countryscroll,text=info[self.list1.GetStringSelection()][keys[i]][1],fontsize=18)
        tempsizer.Add(temp,1,wx.EXPAND|wx.LEFT|wx.RIGHT,40)
      
temp=DisplayText(self.countryscroll,text=info[self.list2.GetStringSelection()][keys[i]][1],fontsize=18)
        tempsizer.Add(temp,1,wx.EXPAND|wx.LEFT|wx.RIGHT,40)
        self.countrysizer.Add(tempsizer,0,wx.EXPAND|wx.ALL,5)

    self.countryscroll.Refresh()
    self.countrysizer.Layout()

class DisplayText(wx.Panel):
  def __init__(self,parent,text='',fontsize=10,size=(-1,-1)):
    wx.Panel.__init__(self,parent,-1,size=size)
    self.SetBackgroundColour('grey')

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

    self.font=wx.Font(fontsize,wx.SWISS,wx.NORMAL,wx.NORMAL)
    self.text=text

  def SetText(self,text):
    self.text=text
    self.Refresh()

  def OnPaint(self,event):
    dc=wx.PaintDC(self)
    dc.SetFont(self.font)

    if self.text!='':
      w,h=self.GetSize()

      verticalspacing=0

      words=self.text.split()
      lines=[' '+words[0]]
      currentline=0
      for i in range(1,words.__len__()):
        if dc.GetTextExtent(lines[currentline]+' '+words[i])[0]<w:
          if lines[currentline]!='':
            lines[currentline]+=' '
          lines[currentline]+=words[i]
        else:
          lines+=[words[i]]
          currentline+=1

self.GetContainingSizer().SetItemMinSize(self,(0,(dc.GetTextExtent(lines[0])[1]+verticalspacing)*lines.__len__()))

      for i in range(0,lines.__len__()):
        tw,th=dc.GetTextExtent(lines[i])
        dc.DrawText(lines[i],0,(th+verticalspacing)*i)

  def OnSize(self,event):
    self.Refresh()

app=wx.App()
f=frame()
app.MainLoop()

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Sizers-again-tp4437363p4437363.html
Sent from the wxPython-users mailing list archive at Nabble.com.

You need to send your sample code as an attachment. Copy/pasting just does not work when mailers/archivers/etc. mangle the text part of the message to wrap lines. MakingSampleApps - wxPyWiki

Without being able to run your code all I can do is guess, and my guess is that you are having this problem because the paint event handler is the wrong place and time to do sizing related stuff. The size event handlers (where auto layout is done) of the window and parents have already been called, and so your changes won't take effect until the next size event. You definitely do not want to call Layout in every paint event either, because there will be lots of paint events where the layout needs do not change.

So at a minimum you probably want to set some layoutNeeded flag in the paint handler, but only if the sizing needs changed from the last time. And then have an EVT_IDLE handler that checks that flag.

A better approach would be to do your size calculations and settings in the EVT_SIZE handler, calling evt.Skip() if you want the default autolayout to still happen.

The best approach would be to derive your DisplayText class from wx.PyPanel and then override the DoGetBestSize method. That method should calculate the best size desired for drawing the contents of the panel and return it as a wx.Size object. The sizers will then automatically use that value when calculating layout. You may still need an extra Layout called after the size events have completed however.

···

On 5/29/11 9:30 AM, Hawkwing wrote:

self.GetContainingSizer().SetItemMinSize(self,(0,(dc.GetTextExtent(lines[0])[1]+verticalspacing)*lines.__len__()))
in DisplayText is supposed to make is so the sizer will be big enough for
the text, but it seems like it's always 1 refresh behind.
If you run the code and restore and maximize it, the DisplayText is the size
it should have been when you restored it.

--
Robin Dunn
Software Craftsman

Sorry about the code, I'm a little new. I'll read through the sample apps
page when I have time.
Thank you for the solution. I ended up deriving it from PyPanel and making
my own DoGetBestSize. I had to call it from the OnSize function and do an
extra Layout, but it doesn't really matter to me if it's inefficient right
now, I'm just glad it works.

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Sizers-again-tp4437363p4440326.html
Sent from the wxPython-users mailing list archive at Nabble.com.