[wxpython-users] wx.StaticLine in a sizer - resizes poorly

581 views
Skip to first unread message

brian....@seagate.com

unread,
Jul 14, 2008, 3:00:24 PM7/14/08
to wxpytho...@lists.wxwidgets.org

I have a program that requires a legend, I have created a frame that pops up and explains what each heading for the rows and columns of a table mean. I want to use a static line to bisect both the header from the data, and the row side from the column side. Unfortunately, when I do this, the line gets resized to fit the sizer becoming unbearably thick. Code follows:

import wx

class counter():
   def __init__(self,start=None):
      if start == None:
         self.value = -1
      else:
         self.value = start-1
   def next(self):
      self.value += 1
      return self.value

class KeyFrame(wx.Frame):
   """A frame for displaying legend information"""
   def __init__(self,parent):        
      wx.Frame.__init__(self, parent, -1, "Key for Table")
      panel = wx.Panel(self)
      rowpanel = wx.Panel(panel)
      rowsizer = wx.FlexGridSizer(cols=2)
      colpanel = wx.Panel(panel)
      colsizer = wx.FlexGridSizer(cols=2)
      sizer = wx.GridBagSizer(hgap=1,vgap=1)
     
      # Get the keys from legend, if not defined, default to an empty dictionary
      rowKey = {}        # for simplicity assume there is no data in the key yet.
      colKey = {}
      rowList=["1","2"]
      colList=["A","B","C"]
 
      # Create the key heading
      sizer.Add(wx.StaticText(panel, wx.ID_ANY, "Rows"), pos = (0,1), flag=wx.ALIGN_CENTER)
      sizer.Add(wx.StaticText(panel, wx.ID_ANY, "Columns"), pos = (0,3), flag=wx.ALIGN_CENTER)

      ############################# These are the lines I want help with #############
      sizer.Add(wx.StaticLine(panel), pos=(1,0), span=(1,5), flag=wx.ALL|wx.CENTER|wx.EXPAND) # a separation line
      sizer.Add(wx.StaticLine(panel,style=wx.LI_VERTICAL), pos=(0,2), span=(3,1), flag=wx.ALL|wx.CENTER|wx.EXPAND)
      ########################################################################

      sizer.AddGrowableRow(3)
      sizer.Add(wx.StaticText(panel, wx.ID_ANY, ""), pos = (0,0)) # add a bit of space to the sides
      sizer.Add(wx.StaticText(panel, wx.ID_ANY, ""), pos = (0,4))
     
      # Create the key body
      row=counter(0) # display the row key
      for header in rowList:
         rowsizer.Add(wx.StaticText(rowpanel, wx.ID_ANY, header+": "), 1, wx.ALIGN_RIGHT)
         rowsizer.Add(wx.StaticText(rowpanel, wx.ID_ANY, str(rowKey.setdefault(header, "Undefined"))), 1, wx.ALIGN_LEFT)
      rowpanel.SetSizer(rowsizer)
      sizer.Add(rowpanel,pos=(2,1))

      col=counter(0) # display the col key
      for header in colList:
         colsizer.Add(wx.StaticText(colpanel, wx.ID_ANY, header+": "), 1, wx.ALIGN_RIGHT)
         colsizer.Add(wx.StaticText(colpanel, wx.ID_ANY, str(colKey.setdefault(header, "Undefined"))), 1, wx.ALIGN_LEFT)
      colpanel.SetSizer(colsizer)
      sizer.Add(colpanel,pos=(2,3))
     
      panel.SetSizer(sizer)
      sizer.Fit(self)

myApp = wx.App(False)
frame = KeyFrame(None).Show()
myApp.MainLoop()

I found a custom object that will force the line to only grow in the specified direction. Adding this:

class NewStaticLine(wx.StaticLine):
   """A class that just defines the thin expandable separator line used in KeyFrame,
      the only difference between this an a standard wx.StaticLine is that this one allows
      resizing in the length direction, while stopping any in the width direction."""
   def __init__(self, *args, **kwargs):
      size = kwargs.get("size", wx.Size(100, 100))
      style = kwargs.get("style", wx.LI_HORIZONTAL)
      d = wx.StaticLine.GetDefaultSize()
      if style & wx.LI_HORIZONTAL:
         kwargs["size"] = (size[0], d)
      elif style & wx.LI_VERTICAL:
         kwargs["size"] = (d, size[1])
      wx.StaticLine.__init__(self, *args, **kwargs)
      self.Bind(wx.EVT_SIZE, self.OnSize)

   def OnSize(self, evt):
      """Resizes are only to be done in the "long" direction"""
      d = wx.StaticLine.GetDefaultSize()
      style = self.GetWindowStyleFlag()
      size = wx.StaticLine.GetSize(self)
      if style & wx.LI_HORIZONTAL:
         size = (size.width, d)
      elif style & wx.LI_VERTICAL:
         size = (d, size.height)

and changing the two lines in question to use "NewStaticLine" instead of "wx.StaticLine" makes nice thin, but off-center lines.

How does one add a static line that grows only in one direction and can be centered?

-Brian

Brian Fett
1280 Disc Dr
SHK224
Shakopee, MN 55379

Phone: (952)402-2595
Brian....@seagate.com

Robin Dunn

unread,
Jul 18, 2008, 3:08:59 PM7/18/08
to wxpytho...@lists.wxwidgets.org
brian....@seagate.com wrote:
>
> Actually, if you run the code supplied in the original post (both the
> supplied and slightly altered versions), the lines cross just fine. They
> just look terrible in some other way.

Sorry, I wasn't thinking about wx.GridBagSizer with spanning when I
wrote that, and I obviously hadn't looked at the code yet. The nested
sizer trick should still work however. See the attached. In this
example the vertical static line would normally be expanded to be very
wide (on Windows at least) because of the button in the same column.
Putting it in a boxsizer and centering it within that takes care of the
problem.


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

crossedlines.xrc

brian....@seagate.com

unread,
Jul 14, 2008, 3:50:49 PM7/14/08
to wxpytho...@lists.wxwidgets.org

Thank you, but there is a problem with this solution: static lines are allowed to cross, sizers (unless I am understanding things wrong) are not allowed to cross. Since I would like the lines to make a cross formation in the center, I don't think that the sizer container option will suffice.

Are there any other options that will work for such an application?

-Brian

Brian Fett
1280 Disc Dr
SHK224
Shakopee, MN 55379

Phone: (952)402-2595
Brian....@seagate.com



Robin Dunn <ro...@alldunn.com>
Sent by: wxpython-us...@lists.wxwidgets.org
No Phone Info Available

07/14/2008 02:29 PM


To
wxpytho...@lists.wxwidgets.org
cc
Subject
Re: [wxpython-users] wx.StaticLine in a sizer - resizes poorly





brian....@seagate.com wrote:
>
> I have a program that requires a legend, I have created a frame that
> pops up and explains what each heading for the rows and columns of a
> table mean. I want to use a static line to bisect both the header from
> the data, and the row side from the column side. Unfortunately, when I
> do this, the line gets resized to fit the sizer becoming unbearably
> thick. Code follows:
>

>
> How does one add a static line that grows only in one direction and can
> be centered?

The grid sizers have this problem in that they expend in both directions
to fill the cell.  The workaround is to put the static line in a box
sizer and then put the box sizer in the grid sizer.  You can make the
grid sizer expand the box sizer like normal, but the box sizer will only
need to expand the line in the one direction.




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

_______________________________________________
wxpython-users mailing list
wxpytho...@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

brian....@seagate.com

unread,
Jul 14, 2008, 5:01:35 PM7/14/08
to wxpytho...@lists.wxwidgets.org

Actually, if you run the code supplied in the original post (both the supplied and slightly altered versions), the lines cross just fine. They just look terrible in some other way.

It seems that with a GridBagSizer only the endpoints of the staticline need to be unoccupied. If this is abnormal behavior (I can see how it would be) then I don't know what to say.

-Brian

Brian Fett
1280 Disc Dr
SHK224
Shakopee, MN 55379

Phone: (952)402-2595
Brian....@seagate.com



Robin Dunn <ro...@alldunn.com>
Sent by: wxpython-users-bounces+brian.d.fett=seaga...@lists.wxwidgets.org
No Phone Info Available

07/14/2008 03:16 PM


To
wxpytho...@lists.wxwidgets.org
cc
Subject
Re: [wxpython-users] wx.StaticLine in a sizer - resizes poorly


brian....@seagate.com wrote:
>
> Thank you, but there is a problem with this solution: static lines are
> allowed to cross, sizers (unless I am understanding things wrong) are
> not allowed to cross. Since I would like the lines to make a cross
> formation in the center, I don't think that the sizer container option
> will suffice.

Well, you wouldn't be able have them cross with just a grid sizer (of
any type) either because the sizers don't work that way.  You'll
probably have to manually maintain their position and size.
Reply all
Reply to author
Forward
0 new messages