Simplifying objects
-
I have working on a form in QT Creator 7.0.2, using Python.
I have the following files:
- form.ui (created by the system)
- Stelcor.pyproject (created by the system)
- widget.py (created by the system)
- definitions.py (created by myself)
- savecsv.py (created by myself)
I have gotten the code to work as I want it to but would like to explore any method of simplification that I cannot yet see.
I have several lines such as these...
self.lunit = self.window.findChild(QLineEdit, 'lunit') self.lfile = self.window.findChild(QLineEdit, 'lfile') self.kunit = self.window.findChild(QLineEdit, 'kunit') self.sfile = self.window.findChild(QLineEdit, 'sfile') self.junit = self.window.findChild(QLineEdit, 'junit') self.ofile = self.window.findChild(QLineEdit, 'ofile') self.unitz = self.window.findChild(QLineEdit, 'unitz') self.filez = self.window.findChild(QLineEdit, 'filez')
The QLineEdit names on the form are necessary as they are; they help me relate this form with another program I am working on.
I am wondering if there is a way I can set up the self.lunit et al in such a way that I can loop through them, rather than have lots of lines, as I currently do.
As I say, the QLineEdit names need to remain, but the self.xxxx can be anything - I am not emotional about these.
Any ideas?
-
I have working on a form in QT Creator 7.0.2, using Python.
I have the following files:
- form.ui (created by the system)
- Stelcor.pyproject (created by the system)
- widget.py (created by the system)
- definitions.py (created by myself)
- savecsv.py (created by myself)
I have gotten the code to work as I want it to but would like to explore any method of simplification that I cannot yet see.
I have several lines such as these...
self.lunit = self.window.findChild(QLineEdit, 'lunit') self.lfile = self.window.findChild(QLineEdit, 'lfile') self.kunit = self.window.findChild(QLineEdit, 'kunit') self.sfile = self.window.findChild(QLineEdit, 'sfile') self.junit = self.window.findChild(QLineEdit, 'junit') self.ofile = self.window.findChild(QLineEdit, 'ofile') self.unitz = self.window.findChild(QLineEdit, 'unitz') self.filez = self.window.findChild(QLineEdit, 'filez')
The QLineEdit names on the form are necessary as they are; they help me relate this form with another program I am working on.
I am wondering if there is a way I can set up the self.lunit et al in such a way that I can loop through them, rather than have lots of lines, as I currently do.
As I say, the QLineEdit names need to remain, but the self.xxxx can be anything - I am not emotional about these.
Any ideas?
-
@GaryN
If you are happy to have an array/list for yourself...
variable (instead of individual variables as you have now), you can use a Pythonenumerate
loop to set up this array from a list of the names of theQLineEdit
s conveniently. -
@GaryN
If you are happy to have an array/list for yourself...
variable (instead of individual variables as you have now), you can use a Pythonenumerate
loop to set up this array from a list of the names of theQLineEdit
s conveniently.@JonB
Just to show what I tried (based upon information I found elsewhere):def initialise(self): dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit', 'ofile', 'unitz', 'filez'] fv_dict = {} for dataItem in dataItems: self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
This generates the error:
AttributeError: 'Form' object has no attribute 'fv_dict'
-
@JonB
Just to show what I tried (based upon information I found elsewhere):def initialise(self): dataItems = ['lunit', 'lfile', 'kunit', 'sfile', 'junit', 'ofile', 'unitz', 'filez'] fv_dict = {} for dataItem in dataItems: self.fv_dict[dataItem] = self.window.findChild(QLineEdit, dataItem)
This generates the error:
AttributeError: 'Form' object has no attribute 'fv_dict'
@GaryN said in Simplifying objects:
fv_dict = {}
self.fv_dic[]
Read the error message, think about it. This is basic Python.
Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with
enumerate
, yours is fine. -
@GaryN said in Simplifying objects:
fv_dict = {}
self.fv_dic[]
Read the error message, think about it. This is basic Python.
Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with
enumerate
, yours is fine. -
@GaryN said in Simplifying objects:
fv_dict = {}
self.fv_dic[]
Read the error message, think about it. This is basic Python.
Other than that you have the right code. If you prefer a dictionary over a list for accessing your widgets, I was going to do it with
enumerate
, yours is fine.