devtk
devtk

Reputation: 2179

How to adjust border around StaticText object programatically?

How do I adjust the border of a StaticText object programatically given the StaticText object? It seems there is a SizerItem associated with this object, but I cannot figure out how to find it.

I see the StaticText object has a GetBorder() function, but this function returns wx.NO_BORDER, so it's not what I'm looking for.

Minimal working example:

import wx
import wx.xrc


class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(
            self,
            parent,
            id=wx.ID_ANY,
            title=wx.EmptyString,
            pos=wx.DefaultPosition,
            size=wx.Size(500, 300),
            style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL,
        )

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        box_sizer = wx.BoxSizer(wx.VERTICAL)

        labeled_box_sizer = wx.StaticBoxSizer(
            wx.StaticBox(self, wx.ID_ANY, u"label"), wx.VERTICAL
        )

        self.m_text_top = wx.StaticText(
            labeled_box_sizer.GetStaticBox(),
            wx.ID_ANY,
            u"Border = 0",
            wx.DefaultPosition,
            wx.DefaultSize,
            0,
        )
        self.m_text_top.Wrap(-1)

        labeled_box_sizer.Add(self.m_text_top, 0, wx.ALL, 0)

        self.m_text_bottom = wx.StaticText(
            labeled_box_sizer.GetStaticBox(),
            wx.ID_ANY,
            u"Border = 5",
            wx.DefaultPosition,
            wx.DefaultSize,
            0,
        )
        self.m_text_bottom.Wrap(-1)

        labeled_box_sizer.Add(self.m_text_bottom, 0, wx.ALL, 5)

        box_sizer.Add(labeled_box_sizer, 0, wx.ALL, 5)

        self.SetSizer(box_sizer)
        self.Layout()

        self.Centre(wx.BOTH)

    def __del__(self):
        pass


app = wx.App()
window = MyFrame(None)
window.Show()

text = window.m_text_top

# text.SetBorder(5)  # <-- no such method exists

app.MainLoop()

I've tried GetSizer() and GetContainingSizer() functions with no such luck.

Upvotes: 0

Views: 331

Answers (1)

RobinDunn
RobinDunn

Reputation: 6306

You should be able to do get the wx.SizerItem like this:

item = text.GetContainingSizer().GetItem(text)

If you want to just keep a reference to the sizer item instead of fetching it each time, it is returned from the sizer's Add method when you add the static text.

Once you have the item then you can adjust the sizer-specific attributes for that item. Be sure to call the sizer's or containing widget's Layout method when you are done adjusting.

Upvotes: 1

Related Questions