Search code examples
wxpythonxrcwxformbuilder

Using a variable instead of constants for properties in wxFormBuilder


In widgets like the spinctrl or slider box, properties like the inital position, min and max values etc can be set to constants. Is there any way by which they can be specified as variables.

so the code

self.HDSpin = wx.SpinCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS, 0, 10, 0 )

becomes

self.HDSpin = wx.SpinCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS, params.minv, params.maxv, params.defaultv )

This will allow the parameters to be set at run time just before the dialog is shown.


Solution

  • As a workaround, in place of each variable enter a unique number: eg: 123401,123402,123403 etc. Once the "gui.py" autogenerated file is created, run a script with sed that replaces each number with the corresponding variable. I use the same to make the default strings runtime determined as opposed to hardcoded. With strvar01,strvar02 in the default string textboxes

    #!/bin/bash
    
    FILE="gui.py"
    
    sed -i 's/123401/HDMin/g' "$FILE"
    sed -i 's/123402/HDDefault/g' "$FILE"
    sed -i 's/123403/HDMax/g' "$FILE"
    sed -i 's/u"strvar01"/DefaultStr1/g' "$FILE"
    

    so the auto-generated code self.Label1 = wx.StaticText(..., u"strvar01", ... )

    becomes: self.Label1 = wx.StaticText(...,DefaultStr1, ... )

    where the value of DefaultStr1 is set at runtime