W. MacTurk
W. MacTurk

Reputation: 170

wxPython - centering text in wx.Notebook tabs?

I'm writing an application using wxPython for the GUI. I'm almost there, but there are some minor aesthetic things that are bugging me. Namely, at the end of the program (the 'results' frame), I have two nested notebooks. I'd like the text for the page-select tabs to be centered in said tab -- not left-aligned as is the default. I've been through the wxPython docs and searched my best, but with no luck. Does anyone know how to do this?

I've attached some minimal code showing the behavior I don't want (namely, left-aligned text in the tabs you select the notebook pages with), as well as a screenshot from the real-live app (which has, naturally, a bit more complicated interface -- but the solution, if it exists, should be something simple, I assume)

import wx

class ResultFrame(wx.Frame):
    def __init__(self, parent):
        super(ResultFrame, self).__init__(parent, -1, title = "Results",
         style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER)
        self.targets = ['these','are','some','strings']
        self.SetMinSize(wx.Size(1590,900))
        self.InitUI()

    def InitUI(self):
        self.mainpnl = wx.Panel(self)
        self.mainpnl.SetSize(wx.Size(1590,900))
        self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.ctrlsizer = wx.BoxSizer(wx.VERTICAL)

        self.restart = wx.Button(self.mainpnl, wx.ID_ANY, "Start Over",\
         size=wx.Size(150,90))
        self.interp = wx.Button(self.mainpnl, wx.ID_ANY, "Help Interpreting\n"
         "Results", size=wx.Size(150,90))
        self.nb = wx.Notebook(self.mainpnl)
        for idx, tar in enumerate(self.targets):
            self.nb.AddPage(wx.Panel(self.nb, -1, size=wx.Size(850,820)), tar)
        self.ctrlsizer.Add(self.restart, 0, wx.ALL, 5)
        self.ctrlsizer.AddStretchSpacer()
        self.ctrlsizer.Add(self.interp, 0, wx.ALL, 5)
        self.mainsizer.Add(self.ctrlsizer, 0, wx.ALL, 0)
        self.mainsizer.Add(self.nb, 1, wx.EXPAND)
        self.mainpnl.SetSizerAndFit(self.mainsizer)

def main():
    app = wx.App()
    frm=ResultFrame(None)
    frm.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

For example, the tab "LOD" should have "LOD" in the middle, not to the left.

Upvotes: 0

Views: 313

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

In wxPython Sizers are used for laying out a window or collection of windows.
A notebook is no different, in that it too, can have its tabs laid out with a sizer or collection of sizers.
Sizers allow both positioning and alignment.

import wx

class ResultFrame(wx.Frame):
    def __init__(self, parent):
        super(ResultFrame, self).__init__(parent, -1, title = "Results",
         style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER)
        #self.targets = ['these','are','some','strings']
        self.SetMinSize(wx.Size(1000,-1))
        self.InitUI()

    def InitUI(self):
        self.mainpnl = wx.Panel(self)
        self.mainpnl.SetSize(wx.Size(1590,900))
        self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.ctrlsizer = wx.BoxSizer(wx.VERTICAL)

        self.restart = wx.Button(self.mainpnl, wx.ID_ANY, "Kubla Khan",\
         size=wx.Size(150,90))
        self.interp = wx.Button(self.mainpnl, wx.ID_ANY, "Samuel Taylor Coleridge\n"
         "Biography", size=wx.Size(150,90))
        self.nb = wx.Notebook(self.mainpnl)
#        for idx, tar in enumerate(self.targets):
#            self.nb.AddPage(wx.Panel(self.nb, -1, size=wx.Size(850,820)), tar)
        tab1 = wx.Panel(self.nb, -1)
        tab2 = wx.Panel(self.nb, -1)

        msg = '''In Xanadu did Kubla Khan
A stately pleasure dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
    Down to a sunless sea.
So twice five miles of fertile ground
With walls and towers were girdled round:
And there were gardens bright with sinuous rills,
Where blossomed many an incense-bearing tree;
And here were forests ancient as the hills,
Enfolding sunny spots of greenery.'''
        t1_text = wx.StaticText(tab1, -1, label=msg)
        t1_butt = wx.Button(tab1, -1, label="Next")
        t1sizer = wx.BoxSizer(wx.VERTICAL)
        t1sizer.Add(t1_text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        t1sizer.Add(t1_butt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
        tab1.SetSizer(t1sizer)
        self.nb.AddPage(tab1,"Stanza 1")

        msg = '''But oh! that deep romantic chasm which slanted
Down the green hill athwart a cedarn cover!
A savage place! as holy and enchanted
As e'er beneath a waning moon was haunted
By woman wailing for her demon lover!
And from this chasm, with ceaseless turmoil seething,
As if this earth in fast thick pants were breathing,
A mighty fountain momently was forced:
Amid whose swift half-intermitted burst
Huge fragments vaulted like rebounding hail,
Or chaffy grain beneath the thresher's flail:
And ’mid these dancing rocks at once and ever
It flung up momently the sacred river.
Five miles meandering with a mazy motion
Through wood and dale the sacred river ran,
Then reached the caverns measureless to man,
And sank in tumult to a lifeless ocean:
And ’mid this tumult Kubla heard from far
Ancestral voices prophesying war!'''
        t2_text = wx.StaticText(tab2, -1, label=msg)
        t2_pbutt = wx.Button(tab2, -1, label="Prev")
        t2_nbutt = wx.Button(tab2, -1, label="Next")
        t2sizer = wx.BoxSizer(wx.VERTICAL)
        t2sizer.Add(t2_text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        t2sizer.Add(t2_pbutt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
        t2sizer.Add(t2_nbutt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
        tab2.SetSizer(t2sizer)
        self.nb.AddPage(tab2,"Stanza 2")
        
        self.ctrlsizer.Add(self.restart, 0, wx.ALL, 5)
        self.ctrlsizer.AddStretchSpacer()
        self.ctrlsizer.Add(self.interp, 0, wx.ALL, 5)
        self.mainsizer.Add(self.ctrlsizer, 0, wx.ALL, 0)
        self.mainsizer.Add(self.nb, 1, wx.EXPAND)
        self.mainpnl.SetSizerAndFit(self.mainsizer)

        t1_butt.Bind(wx.EVT_BUTTON, self.Next)
        t2_nbutt.Bind(wx.EVT_BUTTON, self.Next)
        t2_pbutt.Bind(wx.EVT_BUTTON, self.Prev)

    def Next(self, event):
        p = self.nb.GetSelection()
        p += 1 
        p = min(p, self.nb.GetRowCount())
        self.nb.SetSelection(p)

    def Prev(self, event):
        p = self.nb.GetSelection()
        p -= 1 
        p = max(0, p)
        self.nb.SetSelection(p)

def main():
    app = wx.App()
    frm=ResultFrame(None)
    frm.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

enter image description here

enter image description here

Upvotes: 1

Related Questions