On Sunday, December 09, 2012 11:48:18 PM Shriramana Sharma wrote:
> Hi -- recently I was trying to shorten the following overly verbose
> (IMO) lines to:
> 
>               self . p1xSpin = QSpinBox ()
>               self . p1ySpin = QSpinBox ()
>               self . c1xSpin = QSpinBox ()
>               self . c1ySpin = QSpinBox ()
>               self . c2xSpin = QSpinBox ()
>               self . c2ySpin = QSpinBox ()
>               self . p2xSpin = QSpinBox ()
>               self . p2ySpin = QSpinBox ()
> 
> to:
> 
>               self . p1xSpin, self . p1ySpin, self . c1xSpin, self . c1ySpin,
>               self . c2xSpin, self . c2ySpin, self . p2xSpin, self . p2ySpin,
>               self . pHxSpin, self . pHySpin = ( QSpinBox () for i in range ( 
> 10 ) 
)
> 
> but I got the error:
> 
> Traceback (most recent call last):
>   File "./bezierview-cubic-interp.py", line 327, in <module>
>     mainWindow = MainWindow ()
>   File "./bezierview-cubic-interp.py", line 204, in __init__
>     self . p1xSpin, self . p1ySpin, self . c1xSpin, self . c1ySpin,
> AttributeError: 'MainWindow' object has no attribute 'p1xSpin'
> 

Probably some simple error here causing the line to try to get the attribute 
instead of set it.

>>> class Y(object):                                                            
>>>                                                                             
>>>                                                                             
>>>                                       
...     def __init__(self):
...             self.a, self.b = (QSpinBox() for a in range(2))
...                                                                             
                                                                                
                                                                                
                              
>>>                                                                             
>>>                                                                             
>>>                                                                             
>>>                                       
>>> y = Y()                                                                     
>>>                                                                             
>>>                                                                             
>>>                                       

> Why is this, and is there any other method I can follow to shorten the
> tortuous list of repetitive commands for initializing multiple
> widgets?
> 

If you have a bunch of similarly named attributes then you can easily generate 
them.

>>> class X(object):
...     def __init__(self):
...             for at in ['%s%s%s' % (a,b,c) for a in ('c','p') for b in 
('x','y') for c in (1,2)]:
...                     setattr(self,at,QSpinBox())
... 
>>> x = X()
>>> x.cx1
<PyQt4.QtGui.QSpinBox object at 0x27c8440>
>>> x.cx2
<PyQt4.QtGui.QSpinBox object at 0x27c84d0>                                      
                                                                                
                                                                                
                              


Matt
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to