Yet another Sizer confusion

I've got a dialog which includes a couple of TextCtrls as fields, one read-only, the other where the user enters a string. (Code extract below.)

My problem is that the text-entry field is sometimes too narrow, and as the user enters text it doesn't scroll. I don't care about scrolling so much (though that should be controllable, shouldn't it, without adding a horizontal scrollbar to a one-line-high field?). I just want to make the field wide enough for any likely string (I can predict pretty well in this case). But I don't want it to extend all the way to both edges of the dialog, which looks stupid.

I can't figure out the magic combination for making it *bigger* but not *too big*. (Fooling around with the dialog's self.GetSizeTuple and so on doesn't seem very promising. I tried a second, horizontal sizer around (each) TextCtrl, but maybe I didn't get the right combination of flags for its style for my purpose?

My dialog's __init__ contains these lines, plus others clearly irrelevant to the problem:

wx.Dialog.__init__(self, None, id, style=wx.DEFAULT_DIALOG_STYLE |
               wx.RESIZE_BORDER)
    self.textline1 = wx.StaticText( . . .
    self.wordAsKnown = wx.TextCtrl(self, -1, "") # my read-only field
    self.wordAsKnown.AppendText(wordSyls)
    self.wordAsKnown.SetEditable(0)
    self.textline4 = wx.StaticText( . . .
    self.wordCorrected = wx.TextCtrl(self, -1, "") # my editable field
    self.OKbutton = wx.Button(self, wx.ID_OK, " OK ")
    self.OKbutton.SetDefault()
    self.CancelButton = wx.Button(self, wx.ID_CANCEL, " Cancel ")
    mainsizer = wx.BoxSizer(wx.VERTICAL)
    mainsizer.Add((20, 20), 0)
    mainsizer.Add(self.textline1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 3)
# . . . (more static stuff)
    mainsizer.Add((20,20),0)
    mainsizer.Add(self.wordAsKnown, 0, wx.ALIGN_CENTER_HORIZONTAL, 3)
# . . . (more spacers and static stuff)
    mainsizer.Add(self.wordCorrected, 0, wx.ALIGN_CENTER_HORIZONTAL, 3)
    mainsizer.Add((20,20),0)
    buttonsizer = wx.BoxSizer(wx.HORIZONTAL)
    buttonsizer.Add((30,30),0)
    buttonsizer.Add(self.OKbutton, 0, wx.ALIGN_LEFT)
    buttonsizer.Add((20,30),0)
    buttonsizer.Add(self.CancelButton, 0, wx.ALIGN_RIGHT)
    mainsizer.Add(buttonsizer, 0, wx.ALIGN_CENTER); mainsizer.AddSpacer((20,20),0)
    self.Bind(wx.EVT_BUTTON, self.OnOK, self.OKbutton)
    self.SetSizer(mainsizer); self.SetAutoLayout(True)
    mainsizer.Fit(self)

Everybody thinks Sizers are a headache (and everybody's right) -- but really, the headache is just figuring out and keeping straight *what the style flags apply to* -- what's inside it, what it's inside. I think maybe that's where I'm going wrong, but now I'm too confused to know.

Thanks for any light on the subject.

Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com

Charles Hartman wrote:

I've got a dialog which includes a couple of TextCtrls as fields, one read-only, the other where the user enters a string. (Code extract below.)

My problem is that the text-entry field is sometimes too narrow, and as the user enters text it doesn't scroll. I don't care about scrolling so much (though that should be controllable, shouldn't it, without adding a horizontal scrollbar to a one-line-high field?).

Is this on the Mac? If so then horizontal scrolling has been fixed in 2.5.4.

I just want to make the field wide enough for any likely string (I can predict pretty well in this case). But I don't want it to extend all the way to both edges of the dialog, which looks stupid.

You can do that by telling the item to expand but also giving it a border, perhaps only on just the left and right sides. Another way is to put it in a horizontal box sizer with a proportion=1 and adding a spacer before and after it and then putting that sizer in the main sizer with the expand flag.

Everybody thinks Sizers are a headache (and everybody's right) -- but really, the headache is just figuring out and keeping straight *what the style flags apply to* -- what's inside it, what it's inside.

The flags given to the Add method apply only to the item being added. If that item is a sizer then it can have cascading effects on the items that the subsizer manages, but if you think of the flags as only applying to the item being added then it is fairly easy to keep it straight. Think first of how that item interacts with its sizer and the other items managed by that sizer, and then mentally do a second pass to think about how the items in any sub-sizers are affected, and etc.

A real good way to learn sizers is to play with one of the design tools that has a live update preview mode. Even if you don't end up using the code it generates being able to quickly play and experiment helps with understanding how sizers work.

···

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