Another, final test of code implementing FlexGridSizer with 4 columns

Think have now, hopefully, got it sorted with regards to implementing 4 columns across FlexGridSizer, sizing, fitting, binding, etc. etc. - if you would like to tell me if there’s anything else am missing out on…?

Also implemented it reporting positioning/dimensions of text controls into status bar for testing, FWIW.

#start code
import wx
#from accessible_output import speech

class Example(wx.Frame):
spk = None

def __init__(self, *args, **kwargs):
    #self.spk = speech.Speaker()
    #self.spk.output("init")
    super(Example, self).__init__(*args, **kwargs)
    self.InitUI()
    self.Centre()
    self.CreateStatusBar()
    self.SetStatusText("")
    self.Fit()         #* modified
    self.Show()
    #self.spk.output("shown")
  
def InitUI(self):
    #self.spk.output("initUI")
    try:
        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()
        exitItem = fileMenu.Append(wx.ID_EXIT, "&Quit\tCtrl+Q", "Close the program")
        menuBar.Append(fileMenu, "&File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.quitProgram, exitItem)
       
        sizer = wx.BoxSizer()     #* modified
        panel = wx.Panel(self)
        sizer.Add(panel, 1, wx.EXPAND)      #* modified
       
        vBox = wx.BoxSizer(wx.VERTICAL)
        lStatics = []
        lControls = []
        #normal textboxes
        for I in range(10):
            lStatics.append(wx.StaticText(panel, label="text " + str(I)))
            lControls.append(wx.TextCtrl(panel))
            lControls[I].Bind(wx.EVT_SET_FOCUS, self.tellPos, lControls[I])
        #multiline text boxes, with label above them
        for I in range(5):
            lStatics.append(wx.StaticText(panel, label="Multi-line " + str(I)))
            lControls.append(wx.TextCtrl(panel, style=wx.TE_MULTILINE))
        #radio buttons
        lRBHorizontals = []
        for I in range(5):
            lRBHorizontals.append(wx.BoxSizer(wx.HORIZONTAL))
            lRBHorizontals[I].Add(wx.RadioButton(parent=panel, id=wx.ID_ANY, label=str(I)+"1", style=wx.RB_GROUP), flag=wx.LEFT, border=10)
            lRBHorizontals[I].Add(wx.RadioButton(parent=panel, id=wx.ID_ANY, label=str(I)+"2"), flag=wx.CENTER, border=10)
            lRBHorizontals[I].Add(wx.RadioButton(parent=panel, id=wx.ID_ANY, label=str(I)+"3"), flag=wx.RIGHT, border=10)
            lStatics.append(wx.StaticText(panel, label="radio button grouping " + str(I)))
            lControls.append(lRBHorizontals[I])
        #more text boxes
        for I in range(11, 16):
            lStatics.append(wx.StaticText(panel, label="text " + str(I)))
            lControls.append(wx.TextCtrl(panel))
            lControls[len(lControls)-1].Bind(wx.EVT_SET_FOCUS, self.tellPos, lControls[len(lControls)-1])
        #add all statics and controls to fgs
        iRows = 1
        if (len(lStatics)/2) != (float(len(lStatics))/2):
            iRows = len(lStatics) / 2 + 1
            sOut = str(len(lStatics)) + " statics / 2 = " + str(iRows)
        else:
            iRows = len(lStatics) / 2
            sOut = str(len(lStatics)) + " statics / 2 = " + str(iRows)
        #dlg = wx.MessageDialog(parent=self, message=sOut, caption="iRows", style=wx.OK)
        #dlg.ShowModal()
        #dlg.Destroy()
        fgs = wx.FlexGridSizer(rows=iRows, cols=4, vgap=9, hgap=25)
        for I in range(len(lStatics)):
            fgs.AddMany([(lStatics[I]), (lControls[I], 1, wx.EXPAND)])

#add fgs to vBox
vBox.Add(fgs, flag=wx.ALIGN_CENTER|wx.CENTER|wx.ALL, border=10) #* modified
#buttons at bottom
hBoxButtons = wx.BoxSizer(wx.HORIZONTAL)
btn1 = wx.Button(panel, label=’&Ok’, size=(70, 30))
btn1.Bind(wx.EVT_SET_FOCUS, self.tellPos, btn1)
hBoxButtons.Add(btn1)
btn2 = wx.Button(panel, label=’&Close’, size=(70, 30))
btn2.Bind(wx.EVT_SET_FOCUS, self.tellPos, btn2)
hBoxButtons.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)
vBox.Add(hBoxButtons, flag=wx.ALIGN_CENTER|wx.CENTER|wx.ALL, border=10) #* modified
panel.SetSizer(vBox)

        self.SetSizer(sizer)                #* modified

        self.Bind(wx.EVT_BUTTON, self.nothingHappening, btn1)
        self.Bind(wx.EVT_BUTTON, self.quitProgram, btn2)
    except Exception as exc:
        dlg = wx.MessageDialog(parent=self, message=str(exc.args), caption="Error message", style=wx.OK)
        dlg.ShowModal()
        dlg.Destroy()
        #self.spk.output("exception occurred - " + str(exc))
    finally:
        sNada = ""
        #self.spk.output("initUI done?")
#end of initUI

def nothingHappening(self, evt):
    dlg = wx.MessageDialog(parent=self, message="You clicked a button", caption="Button clicked", style=wx.OK)
    dlg.ShowModal()
    dlg.Destroy()
#end of nothingHappening

def quitProgram(self, evt):
    self.Close()
#end of quitProgram

def tellPos(self, event):
    sOut = "position " + str(event.GetEventObject().Position)
    sOut += " dimensions " + str(event.GetEventObject().Size)
    #self.spk.output(sOut, interrupt=True)
    if self.StatusBar != None: self.SetStatusText(sOut)
#end of tellPos

#end of class Example

import wx.lib.inspection
if name == ‘main’:
app = wx.App()
Example(None, title=“Lots of fields/controls”)
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
#end code

Jacob Kruger
Blind Biker
Skype: BlindZA
‘…fate had broken his body, but not his spirit…’

Seems to basically look and function alright to me as far as sizers are concerned.

Noticed the OK and Close buttons height don’t match.
Also one of the radio button groups is offcentered and makes it look a bit strange.

offcenter_radiobox.png

See now that when add them to their containing BoxSizer, I am specifying slightly differently for their addition - would guess the border value is changing it and not sure why I used different code - copy/paste…

hBoxButtons.Add(btn1)

hBoxButtons.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)

Which of the radio button groupings?

I am adding them using a for loop, so same code all roung, unless it’s their label text possibly causing different alignment, or different column/row companion controls, or something?

Thanks

Jacob Kruger
Blind Biker
Skype: BlindZA
‘…fate had broken his body, but not his spirit…’

···

----- Original Message -----

From:
Metallicow

To: wxpython-users@googlegroups.com

Cc: jacob@blindza.co.za

Sent: Wednesday, January 01, 2014 03:31 PM

Subject: Re: Another, final test of code implementing FlexGridSizer with 4 columns

Seems to basically look and function alright to me as far as sizers are concerned.

Noticed the OK and Close buttons height don’t match.
Also one of the radio button groups is offcentered and makes it look a bit strange.

The 0 group, the same one in the attached image.
I am thinking it is related to the sizer flag wx.EXPAND. When resizing the frame, the sizers like to intelligently compress the widgets. and the radio button moves back up. It is only occuring on group 0 tho, which is in the 4th column.
This is a bit strange as it has the same length of chars as all the other radio group buttons and only the middle radio is affected.
Not sure how to fix that right off hand, but I would try adding a blank spacer when starting new groups of types of controls. That might work. Or fiddle with the flags a bit.
Robin might know what is going on there by looking at it…

···

On Wednesday, January 1, 2014 8:04:14 AM UTC-6, Jacob Kruger wrote:

See now that when add them to their containing BoxSizer, I am specifying slightly differently for their addition - would guess the border value is changing it and not sure why I used different code - copy/paste…

hBoxButtons.Add(btn1)

hBoxButtons.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)

Which of the radio button groupings?

I am adding them using a for loop, so same code all roung, unless it’s their label text possibly causing different alignment, or different column/row companion controls, or something?