Search code examples
python-3.xuser-interfacewxpython

wxPython Label placement on wx.TextCtrl() text inputs


Good Day, i am using wxPython and want to have some text inputs, like in this sample:

import wx

class MainWindow(wx.Frame):
   def __init__(self, parent, title):
       wx.Frame.__init__(self, parent, title=title)
       self.CreateStatusBar() # A Statusbar in the bottom of the window

       #Input Fields
       inputsdict = {  0:"A",
                       1:"B",
                       2:"C"
                       }
       defaultvalue= { 0:"Some Text",
                       1:"Some Text",
                       2:"Some Text"
                       }

       # the edit control - one line version.
       #self.paralbl = wx.StaticText(self, label="Filter Parameters")
       self.entryboxes = wx.BoxSizer(wx.VERTICAL)
       self.entrys=[]
       for item in inputsdict:
           self.lblname = wx.StaticText(self, label=inputsdict[item])
           self.entrys.append(wx.TextCtrl(self,value=defaultvalue[item]))
           self.entryboxes.Add(self.lblname, 1, wx.EXPAND|wx.ALL,0)
           self.entryboxes.Add(self.entrys[item], 1, wx.EXPAND|wx.BOTTOM,10)
           self.Bind(wx.EVT_TEXT, self.EvtText, self.entrys[item])
           self.Bind(wx.EVT_CHAR, self.EvtChar, self.entrys[item])

       #Layout
       # Use some sizers to see layout options
       self.sizer = wx.BoxSizer(wx.HORIZONTAL)
       self.sizer.Add(self.entryboxes, 2,wx.EXPAND)


       #Layout sizers
       self.SetSizer(self.sizer)
       self.SetAutoLayout(1)
       self.sizer.Fit(self)
       self.Show()

   def EvtChar(self, event):
       pass
   def EvtText(self, event):
       pass

FilterTool = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
wnd = MainWindow(None, "Input Field Sample")    # A Frame is a top-level window.

FilterTool.MainLoop()

The result looks like this:

enter image description here

That is almost what i want but notice that the labels are far more near the entry fields above them than the ones they refere to. I would expect, that:

self.entryboxes.Add(self.lblname, 1, wx.EXPAND|wx.ALL,0)
self.entryboxes.Add(self.entrys[item], 1, wx.EXPAND|wx.BOTTOM,10)

adds a Border to the bottom of the entry and set the border between label and entry field to 0 so that they are touching. But as long as the displayspace in general is not overflown, just the space between label and corresponding entry field is growing.

How can i achieve that the label gets closer to ist corresponding entry box?

Python Version is: Python 3.7.5
Wx Version is: 4.0.6 gtk3 (phoenix) wxWidgets 3.0.4

Solution

  • self.entryboxes.Add(self.lblname, 1, wx.EXPAND|wx.ALL,0)
    self.entryboxes.Add(self.entrys[item], 1, wx.EXPAND|wx.BOTTOM,10)
    
    

    does take care of this but because i had set each their relative weight to 1 the sizes of the resulting boxes are also the same. To solve this i just had to change the weight of the textbox to something greater for example:

    self.entryboxes.Add(self.lblname, 1, wx.EXPAND|wx.ALL,0)
    self.entryboxes.Add(self.entrys[item], 3, wx.EXPAND|wx.BOTTOM,10)
    
    

    The result looks like this:

    enter image description here