Auto-expand TextCtrl to fill Notebook Tab

I’m new to Python and having trouble with fully understanding the BoxSizer all the other “sizer’s” and “layout’s” etc.

I have a TextCtrl that is connected to a Notebook tab, but the TextCtrl window does not fill the whole the tab space available.

I’ve got a split pannel with the right half filled with the notebook class. I’ve read where the attaching the textctrl directly into the notebook should fill automatically. However, in my case it does not.

What is it that I’m doing wrong?

Thank you!!

class MainWindow(wx.Frame):
def init(self, parent, title):
self.dirname=’’

A “-1” in the size parameter instructs wxWidgets to use the default size.

     # In this case, we select 200px width and the default height.
    wx.Frame.__init__(self, parent)

    self.topSizer = wx.BoxSizer(wx.HORIZONTAL)
    sp = wx.SplitterWindow(self)
    p1 = wx.Panel(sp, style=wx.SUNKEN_BORDER)
    p2 = wx.Panel(sp, style=wx.SUNKEN_BORDER)

sp.SplitVertically(p1, p2, 150)

    p1Sizer = wx.BoxSizer(wx.VERTICAL)
    self.treelist = TreeFrame(p1,1,wx.DefaultPosition,(-1,-1),wx.TR_HAS_BUTTONS)
    p1Sizer.Add(self.treelist, 1, wx.ALL|wx.EXPAND, 0)
    p1.SetSizer(p1Sizer)

notebook = Notebook(p2)

self.termOut = wx.TextCtrl(notebook.tabOne, -1, “Under Test”,style=wx.TE_MULTILINE|wx.TE_READONLY)
self.term1 = kbSerialDevice_1.SerialDevice(None,-1,text=self.termOut)

p2Sizer = wx.BoxSizer(wx.VERTICAL)

p2Sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 0)

p2.SetSizer(p2Sizer)

self.topSizer.Add(sp,1,wx.EXPAND)
self.SetSizer(self.topSizer)
self.SetAutoLayout(True)
self.SetSizeHints(400,500)
self.SetSize(wx.Size(600, 400))

class Notebook(wx.Notebook):
“”"
Notebook class
“”"

help.txt (2 KB)

···

#----------------------------------------------------------------------
def init(self, parent):
wx.Notebook.init(self, parent, id=wx.ID_ANY, style=
wx.BK_DEFAULT
)

Create the first tab

      self.tabOne = TabPanel(self)
      self.tabOne.SetBackgroundColour("Gray")

self.AddPage(self.tabOne, “Control”)

      # Create and add the second tab
      self.tabTwo = TabPanel(self)
      self.AddPage(self.tabTwo, "Data Channel")

Create and add the third tab

      self.tabThree = TabPanel(self)
      self.AddPage(self.tabThree, "Printer Channel")

I'm new to Python and having trouble with fully understanding the BoxSizer
all the other "sizer's" and "layout's" etc.
I have a TextCtrl that is connected to a Notebook tab, but the TextCtrl
window does not fill the whole the tab space available.

I think one problem is forcing yourself to use language more carefully
(this sounds pedantic, but bear with me). When you say that the TextCtrl
is "connected to" the notebook tab, what does that really mean? Because,
in looking at your code, what is actually the case is that TextCtrl is
sitting on a panel that *is* a page (or "tab") in your Notebook. That's
the problem...

I've got a split pannel with the right half filled with the notebook
class. I've read where the attaching the textctrl directly into the
notebook should fill automatically. However, in my case it does not.

Yes, it should, but you *didn't* put the TextCtrl directly as a page. You
put it into a panel first. The *panel* is filling the Notebook page; the
textCtrl (on that panel) is not. See here:

        self.termOut = wx.TextCtrl(notebook.tabOne, -1, "Under
Test",style=wx.TE_MULTILINE|wx.TE_READONLY)

This shows the parent of the textCtrl is notebook.tabOne. I then had to
look in the rest of your code to see what that was:

      def __init__(self, parent):
          wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
                               wx.BK_DEFAULT
                               )

          # Create the first tab
          self.tabOne = TabPanel(self)

Aha, ok it is a "tab panel", whatever that is. I guess you made a class
called TabPanel, which I'm guessing is a panel of some sort. But why do
you need that if all you want is the TextCtrl to take up all the space of
the notebook page?

Instead, do:

self.termOut = wx.TextCtrl(notebook, -1, "Under
Test",style=wx.TE_MULTILINE| wx.TE_READONLY)

notebook.AddPage(self.termOut)

···

On Tue, Aug 27, 2013 at 7:18 PM, Hanna Lee <hannavita999@gmail.com> wrote:

Excellent! Thank you!

Somewhere in my investigation, I mistakenly reasoned that the “panel” (you correctly assumed) was where I needed to put my TextCtrl. It looks like I added another layer.

Using your suggestion to remove the “panel” automatically fills the TextCtrl into the tab I needed, once I added it.

…and yes, my code language does need much improvement :-/ (working on that…)

Below is part of my simplified working version.

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

    sp = wx.SplitterWindow(self)
    p1 = wx.Panel(sp, style=wx.SUNKEN_BORDER)

    p2 = wx.Panel(sp, style=wx.SUNKEN_BORDER)
    sp.SplitVertically(p1, p2, 150)

self.treelist = TreeFrame(p1,1,wx.DefaultPosition,(-1,-1),wx.TR_HAS_BUTTONS)

size treelist

p1Sizer = wx.BoxSizer(wx.VERTICAL)
p1Sizer.Add(self.treelist, 1, wx.ALL|wx.EXPAND, 0)
p1.SetSizer(p1Sizer)

notebook = wx.Notebook(p2,id=wx.ID_ANY,style=wx.BK_DEFAULT)

    tab1 = wx.TextCtrl(notebook, -1, "Under Test",style=wx.TE_MULTILINE|wx.TE_READONLY)
    self.term1 = kbSerialDevice_1.SerialDevice(None,-1,text=tab1)

tab1.SetBackgroundColour(“Gray”)

    notebook.AddPage(tab1, "Control")

size notebook

p2Sizer = wx.BoxSizer(wx.VERTICAL)
p2Sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 0)

p2.SetSizer(p2Sizer)

size top

    self.topSizer.Add(sp,1,wx.EXPAND)
    self.SetSizer(self.topSizer)
    self.SetAutoLayout(True)

    self.SetSizeHints(400,500)
    self.SetSize(wx.Size(600, 400))

…which leads me to another smaller question. The section I have under “# size top” does not seem to do anything. Am I being redundant? I was just following other examples and obviously expanded on them.

Thank you Again!

···

On Tue, Aug 27, 2013 at 8:02 PM, C M cmpython@gmail.com wrote:

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

On Tue, Aug 27, 2013 at 7:18 PM, Hanna Lee hannavita999@gmail.com wrote:

I’m new to Python and having trouble with fully understanding the BoxSizer all the other “sizer’s” and “layout’s” etc.

I have a TextCtrl that is connected to a Notebook tab, but the TextCtrl window does not fill the whole the tab space available.

I think one problem is forcing yourself to use language more carefully (this sounds pedantic, but bear with me). When you say that the TextCtrl is “connected to” the notebook tab, what does that really mean? Because, in looking at your code, what is actually the case is that TextCtrl is sitting on a panel that is a page (or “tab”) in your Notebook. That’s the problem…

I’ve got a split pannel with the right half filled with the notebook class. I’ve read where the attaching the textctrl directly into the notebook should fill automatically. However, in my case it does not.

Yes, it should, but you didn’t put the TextCtrl directly as a page. You put it into a panel first. The panel is filling the Notebook page; the textCtrl (on that panel) is not. See here:

self.termOut = wx.TextCtrl(notebook.tabOne, -1, “Under Test”,style=wx.TE_MULTILINE|wx.TE_READONLY)

This shows the parent of the textCtrl is notebook.tabOne. I then had to look in the rest of your code to see what that was:

  def __init__(self, parent):
      wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
                           wx.BK_DEFAULT
                           )

Create the first tab

      self.tabOne = TabPanel(self)

Aha, ok it is a “tab panel”, whatever that is. I guess you made a class called TabPanel, which I’m guessing is a panel of some sort. But why do you need that if all you want is the TextCtrl to take up all the space of the notebook page?

Instead, do:

self.termOut = wx.TextCtrl(notebook, -1, “Under Test”,style=wx.TE_MULTILINE| wx.TE_READONLY)

notebook.AddPage(self.termOut)

Below is part of my simplified working version.

Do you have a small runnable sample to attach? It is easier to diagnose
problems if we can run the code and tweak it.

See here:
http://wiki.wxpython.org/MakingSampleApps

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

        sp = wx.SplitterWindow(self)
        p1 = wx.Panel(sp, style=wx.SUNKEN_BORDER)
        p2 = wx.Panel(sp, style=wx.SUNKEN_BORDER)
        sp.SplitVertically(p1, p2, 150)

        self.treelist =
TreeFrame(p1,1,wx.DefaultPosition,(-1,-1),wx.TR_HAS_BUTTONS)

# size treelist
        p1Sizer = wx.BoxSizer(wx.VERTICAL)

        p1Sizer.Add(self.treelist, 1, wx.ALL|wx.EXPAND, 0)
        p1.SetSizer(p1Sizer)
        notebook = wx.Notebook(p2,id=wx.ID_ANY,style=wx.BK_DEFAULT)
        tab1 = wx.TextCtrl(notebook, -1, "Under
Test",style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.term1 = kbSerialDevice_1.SerialDevice(None,-1,text=tab1)
        tab1.SetBackgroundColour("Gray")
        notebook.AddPage(tab1, "Control")

# size notebook
        p2Sizer = wx.BoxSizer(wx.VERTICAL)
        p2Sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 0)
        p2.SetSizer(p2Sizer)
# size top
        self.topSizer.Add(sp,1,wx.EXPAND)
        self.SetSizer(self.topSizer)
        self.SetAutoLayout(True)

        self.SetSizeHints(400,500)
        self.SetSize(wx.Size(600, 400))
...which leads me to another smaller question. The section I have under "#
size top" does not seem to do anything. Am I being redundant? I was just
following other examples and obviously expanded on them.

From this I'm not sure what the problem is; maybe it's obvious but right
now I don't see it. But if you manually drag the corner of the frame to
expand it, does the SplitterWindow get sized correctly then? If so, try
putting self.Layout() at the end of this code.

Attached is a sample code. I’ve removed a serial class module to help reduce excess code.

And yes, the window does seem to expand correctly.

Thank you again for all your great help!

module3.py (14.5 KB)

···

On Wed, Aug 28, 2013 at 10:43 AM, C M cmpython@gmail.com wrote:

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Below is part of my simplified working version.

Do you have a small runnable sample to attach? It is easier to diagnose problems if we can run the code and tweak it.

See here:
http://wiki.wxpython.org/MakingSampleApps

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

    sp = wx.SplitterWindow(self)
    p1 = wx.Panel(sp, style=wx.SUNKEN_BORDER)

    p2 = wx.Panel(sp, style=wx.SUNKEN_BORDER)
    sp.SplitVertically(p1, p2, 150)

self.treelist = TreeFrame(p1,1,wx.DefaultPosition,(-1,-1),wx.TR_HAS_BUTTONS)

size treelist

p1Sizer = wx.BoxSizer(wx.VERTICAL)

    p1Sizer.Add(self.treelist, 1, wx.ALL|wx.EXPAND, 0)
    p1.SetSizer(p1Sizer)

notebook = wx.Notebook(p2,id=wx.ID_ANY,style=wx.BK_DEFAULT)

    tab1 = wx.TextCtrl(notebook, -1, "Under Test",style=wx.TE_MULTILINE|wx.TE_READONLY)
    self.term1 = kbSerialDevice_1.SerialDevice(None,-1,text=tab1)

tab1.SetBackgroundColour(“Gray”)

    notebook.AddPage(tab1, "Control")

size notebook

p2Sizer = wx.BoxSizer(wx.VERTICAL)
p2Sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 0)

p2.SetSizer(p2Sizer)

size top

    self.topSizer.Add(sp,1,wx.EXPAND)
    self.SetSizer(self.topSizer)
    self.SetAutoLayout(True)
    self.SetSizeHints(400,500)
    self.SetSize(wx.Size(600, 400))

…which leads me to another smaller question. The section I have under “# size top” does not seem to do anything. Am I being redundant? I was just following other examples and obviously expanded on them.

From this I’m not sure what the problem is; maybe it’s obvious but right now I don’t see it. But if you manually drag the corner of the frame to expand it, does the SplitterWindow get sized correctly then? If so, try putting self.Layout() at the end of this code.

You're welcome! Are you all set now, or is there still a question? It
seemed like it's working, but you attached the code so I wasn't sure.

···

On Wed, Aug 28, 2013 at 4:59 PM, Hanna Lee <hannavita999@gmail.com> wrote:

Attached is a sample code. I've removed a serial class module to help
reduce excess code.

And yes, the window does seem to expand correctly.

Thank you again for all your great help!

Hanna Lee wrote:

# size top
         self.topSizer.Add(sp,1,wx.EXPAND)
         self.SetSizer(self.topSizer)
         self.SetAutoLayout(True)

         self.SetSizeHints(400,500)
         self.SetSize(wx.Size(600, 400))
...which leads me to another smaller question. The section I have under
"# size top" does not seem to do anything. Am I being redundant? I was
just following other examples and obviously expanded on them.

The SetAutoLayout is redundant, SetSizer does that for you automatically. The last two lines are a little confusing. The SetSizeHints sets the minsize of self, (assuming self is a wx.Frame) but then on the next line you are setting one of the dimensions smaller than that.

···

--
Robin Dunn
Software Craftsman