Can't get wx SizedPanel to align children vertically

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't get wx SizedPanel to align children vertically

Brendan Simon (eTRIX)
I'm using a the SizedPanel class from wx.lib.sized_controls on macOS (Sierra 10.12) and can't seem to get children objects to align vertically.

The code snippet below has 3 panes in a vertical sized panel.  header and footer pane are fixed in size (i.e. do not expand), but the header pane does.  I can get the static text object to align horizontally (e.g. center) but I can't get it to align vertically (e.g. center).  The text always remains at the top of the pane.

Am I doing something wrong?

Or is there a problem with vertical alignment on macOS perhaps?

Thanks, Brendan.

import wx
import wx.lib.sized_controls as wxSC

##============================================================================

class wxServicePanel(wxSC.SizedPanel):
    def __init__(self, parent):
        super().__init__(parent)

        pane = self
        pane.SetSizerType("vertical")
        #pane.SetSizerType("horizontal")
        #pane.SetBackgroundColour("grey")
        self.pane = pane

        ## Row 1
        header_pane = self.header_pane = wxSC.SizedPanel(pane)
        wx.StaticText(header_pane, label="header_pane")
        header_pane.Fit()
        header_pane.SetMinSize(header_pane.GetSize())

        ## Row 2
        main_pane = self.main_pane = wxSC.SizedPanel(pane)
        main_pane.SetBackgroundColour("white")
        st = wx.StaticText(main_pane, label="main_pane")
        st.SetSizerProps(align='center')
        #st.SetSizerProps(valign='bottom')
        #st.SetSizerProps(valign='center', halign='center')
        #st.SetSizerProps(align='center', expand=True, proportion=1)
        main_pane.SetSizerProps(align='center')
        #main_pane.SetSizerProps(align='center', expand=True)
        #main_pane.SetSizerProps(expand=True, proportion=1)
        #main_pane.SetSizerProps(align='center', expand=True, proportion=1)
        main_pane.Fit()
        main_pane.SetMinSize(main_pane.GetSize())

        ## Row 3
        footer_pane = self.footer_pane = wxSC.SizedPanel(pane)
        st = wx.StaticText(footer_pane, label="footer_pane")
        #st.SetSizerProps(halign='right')
        footer_pane.Fit()
        footer_pane.SetMinSize(footer_pane.GetSize())
        footer_pane.SetSizerProps(halign='right')

        ## ensure can't resize dialog to less screen space than the controls need
        self.Fit()
        self.SetMinSize(self.GetSize())

##============================================================================

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can't get wx SizedPanel to align children vertically

Tim Roberts
Brendan Simon (eTRIX) wrote:
I'm using a the SizedPanel class from wx.lib.sized_controls on macOS (Sierra 10.12) and can't seem to get children objects to align vertically.

The code snippet below has 3 panes in a vertical sized panel.  header and footer pane are fixed in size (i.e. do not expand), but the header pane does.  I can get the static text object to align horizontally (e.g. center) but I can't get it to align vertically (e.g. center).  The text always remains at the top of the pane.

Am I doing something wrong?

Yes, although it's subtle.  The code is doing what you asked, but what you asked is not what you wanted.  The StaticText control is the only thing in the main_pane, so it expands to fill the pane.  The control IS vertically centered, occupying the entire space in the pane.  (You can see that because it is colored white.)  The issue is that a static text control does not have the ability to vertically center its own text within its window.  The control is centered, but the text is not.

What you need are spacers, above and below the static text, to suck up all the extra space.  That's normal and easy when you are using sizers directly, but it's kind of hidden with wx.lib.sized_controls.  Something like this seems to work:

        ## Row 2
        main_pane = self.main_pane = wxSC.SizedPanel(pane)
        main_pane.SetBackgroundColour("white")
        spacer = wxSC.SizedPanel(main_pane,-1)
        spacer.SetSizerProps(proportion=1)
        st = wx.StaticText(main_pane, label="main_pane")
        st.SetSizerProps(align='center')
        spacer = wxSC.SizedPanel(main_pane,-1)
        spacer.SetSizerProps(proportion=1)
-- 
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can't get wx SizedPanel to align children vertically

Brendan Simon (eTRIX)
In reply to this post by Brendan Simon (eTRIX)

I'm using a the SizedPanel class from wx.lib.sized_controls on macOS (Sierra 10.12) and can't seem to get children objects to align vertically. Am I doing something wrong?
Yes, although it's subtle.  The code is doing what you asked, but what you asked is not what you wanted.  The StaticText control is the only thing in the main_pane, so it expands to fill the pane.  The control IS vertically centered, occupying the entire space in the pane.  (You can see that because it is colored white.)  The issue is that a static text control does not have the ability to vertically center its own text within its window.  The control is centered, but the text is not. What you need are spacers, above and below the static text, to suck up all the extra space.  That's normal and easy when you are using sizers directly, but it's kind of hidden with wx.lib.sized_controls.  Something like this seems to work:
        ## Row 2         main_pane = self.main_pane = wxSC.SizedPanel(pane)         main_pane.SetBackgroundColour("white")
        spacer = wxSC.SizedPanel(main_pane,-1)         spacer.SetSizerProps(proportion=1)
        st = wx.StaticText(main_pane, label="main_pane")         st.SetSizerProps(align='center')
        spacer = wxSC.SizedPanel(main_pane,-1)         spacer.SetSizerProps(proportion=1)
Tim Roberts, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="I9WoQ5RvAwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">ti...@...
Thanks Tim.  The trick with the spacer does work :) Note: the white area is the background of the middle pane, not of the static text object.  I set the background of the ST object and it's a different region. I changed the widget to a generic button to see if that makes any difference (just in case there was something weird with static text.  I know native macOS buttons don't expand vertically). I can always get the generic button to be horizontally centred (with or without spacers, and using vertical or horizontal sized panels), however I can only get the generic button t be vertically centred if I use the spacer "trick". Is this right? Why does horizontal centring work, but not vertical centring (without spacers)? Is this just a known limitation, quirk or feature or wxWidgets and/or the native GUI framework (macOS cocoa in this case)? I think what I'm wanting to do is have a static text widget, that doesn't expand, but is positioned in the centre of the parent pane (main pane).  As the main pane expands, the static text should get repositioned to the centre.  That's how I think it should work conceptually, but it doesn't (at least for the vertical alignment).  I'm trying to work out whether it is a bug, or whether I've got my wires crossed (or whether it's just a limitation/quirk that's never going to change). Thanks, Brendan.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.