[PyQt] Shortening multiple widget initializations to one line

David Cortesi davecortesi at gmail.com
Mon Dec 10 16:34:56 GMT 2012


On Mon, Dec 10, 2012 at 4:00 AM,  <pyqt-request at riverbankcomputing.com> wrote:
>  is there any other method I can follow to shorten the
> tortuous list of repetitive commands for initializing multiple widgets?

If you don't mind referring to the objects by index number rather
than individual names, you can do this:

    self.myListOfEightSpinners = [ QSpinBox() for i in range(8) ]

It's very "pythonic"! However, judging from the names you have
used, you have X-Y pairs of spinners, in which case there might be
some coding advantage to having four lists of two, as in

    self.p1xyVals = [ QSpinBox(), QSpinBox() ]
    self.p2xyVals = [ QSpinBox(), QSpinBox() ]

etc. Then you could write e.g. a function

    def XYDistance( xySpinBoxPair ) :

and so forth.


More information about the PyQt mailing list