Hubro
Hubro

Reputation: 59333

How can I polish/perfect positioning and appearance of wxPython TextCtrls?

I am having some purely cosmetic issues with wxPython. For instance the relationship between labels and the controls they represent - everything always appears to be at least 2-3 pixels off target and some times worse. In the dialogue I'm currently creating I have changed the font of my text controls to Consolas and put them in a sizer with wx.EXPAND. First of all, once I changed the font the text in the text controls got skewed a few pixels up, presumably because the font is a pixel or two smaller, and when I set wx.EXPAND the text control grew a few pixels in height, making the skewing even worse. Here are some screenshots of before and after the expand flag, respectively:

enter image description here

enter image description here

As a perfectionist with OCD this is a pretty big problem for me. I can't find any way to affect the position of the text in the text control by means of Google, the Wiki or the API.

Say I want to decrease the height of the text controls by a couple of pixels and nudge their text content down a couple of pixels - what would be the most efficient way of performing such nit-picky adjustments?


Here is the code for the dialog.

Upvotes: 4

Views: 515

Answers (3)

Fenikso
Fenikso

Reputation: 9451

wx.EXPAND in grid sizer expands in both directions. You will need nested box sizer to override this behavior.

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)

        self.label = wx.StaticText(self.panel, label="Label")
        self.text = wx.TextCtrl(self.panel)
        self.button = wx.Button(self.panel, label="+", size=(50, 50))


        self.sizer = wx.GridBagSizer(5, 5)
        self.nested = wx.BoxSizer()
        self.sizer.Add(self.label, (0, 0), flag=wx.ALIGN_CENTER_VERTICAL)
        self.sizer.Add(self.nested, (0, 1), flag=wx.EXPAND)
        self.nested.Add(self.text, proportion=1, flag=wx.ALIGN_CENTER)
        self.sizer.Add(self.button, (0, 2))
        self.sizer.AddGrowableCol(1)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

More information is in wxPython mailing list.

Upvotes: 0

VZ.
VZ.

Reputation: 22688

You should leave the height of the text control alone (i.e. neither hard code it nor use wxEXPAND) and simply use wxALIGN_VERTICAL (as you already do, but it gets overridden by wxEXPAND) to align the control properly.

If you want the text control to expand in horizontal direction only, you should do it in the standard way by putting it inside a wxBoxSizer.

Upvotes: 3

ravenspoint
ravenspoint

Reputation: 20457

When you create the text control, you do not need to accept the default height. You can insist the height is whatever you wish. If you leave the other dimensions at their default ( -1 ) then the sizers will look after those things for you, but respect your decision about the height.

tc = wx.TextCtrl(parent, -1, "", 
     wxPoint(-1,-1), wxSize(-1,my_preferred_height_in_pixels))

[ I do not know python well - you should be able to convert this into good python ]

Upvotes: 0

Related Questions