Hello,
I'm a beginner studying coding by myself.
While programming through PYQT5, there is a repetitive code, so I want to reduce it, so I'm asking you a question because it didn't work well.
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(791, 915)
.
.
.#Optimized
.
.
def URLclean(self):
self.lineEdit_1.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
self.lineEdit_4.clear()
self.lineEdit_5.clear()
self.lineEdit_6.clear()
self.lineEdit_7.clear()
self.lineEdit_8.clear()
self.lineEdit_9.clear()
self.lineEdit_10.clear()
The part I want to reduce is def URL clean(self)
part.
LineEdit_1-10 in the corresponding part anyway, and it is repeated through the for statement
def URLclean(self):
a = lineEdit_
for nums in range(1,11):
self.a+nums.clear()
I was going to shorten it like this, but self. It seems that a+nums after the class is not recognized as a variable at all.
How do we solve this?
Thank you.
python class pyqt
def setupUi(self, MainWindow):
# ...
self.lineEdit = [ QLineEdit(self) for _ in range(10) ]
# ...
def UrlClean(self):
for edit in self.lineEdit:
edit.clear()
Rather than writing a variable name such as lineEdit_n
, create as many widgets as necessary in the array lineEdit
and lineEdit[0],
lineEdit[1]
... If you make it like this, it would be good to deal with it all at once, such as for.
© 2024 OneMinuteCode. All rights reserved.