1
フォームにPyQt5
があり、QLineEdits
フィールドが追加されています。PyQt5 - ボタンを押したときにフォームに入力を追加できない
ユーザーがQPushButton
をクリックしたときに、フォームにプログラムでQLineEdits
フィールドを追加したいのですが、アプリケーションを実行してボタンをクリックするとフォームに何も追加されず、エラーが発生します。
どうすれば解決できますか? これは私のコードです:ここでは
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_AnonForm(object):
def setupUi(self, AnonForm):
AnonForm.setObjectName("AnonForm")
AnonForm.resize(500, 410)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.AnonTxt1 = QtWidgets.QLabel(AnonForm)
self.AnonTxt1.setGeometry(QtCore.QRect(20, 10, 60, 20))
self.AnonTxt1.setFont(font)
self.AnonTxt1.setTextFormat(QtCore.Qt.RichText)
self.AnonTxt1.setAlignment(QtCore.Qt.AlignCenter)
self.AnonTxt1.setObjectName("AnonTxt1")
self.AnonGroups = QtWidgets.QLineEdit(AnonForm)
self.AnonGroups.setGeometry(QtCore.QRect(20, 30, 60, 20))
self.AnonGroups.setFont(font)
self.AnonGroups.setMaxLength(6)
self.AnonGroups.setAlignment(QtCore.Qt.AlignCenter)
self.AnonGroups.setObjectName("AnonGroups")
self.AnonGo = QtWidgets.QPushButton(AnonForm)
self.AnonGo.setEnabled(False)
self.AnonGo.setGeometry(QtCore.QRect(400, 26, 80, 28))
self.AnonGo.setFont(font)
self.AnonGo.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.AnonGo.setObjectName("AnonGo")
self.retranslateUi(AnonForm)
QtCore.QMetaObject.connectSlotsByName(AnonForm)
self.AnonGroups.setValidator(QtGui.QRegExpValidator(QtCore.QRegExp('^[0-9]+$')))
self.AnonGroups.textChanged.connect(self.AnonOnpercentInputChg)
self.AnonGo.clicked.connect(lambda: self.AnonGoSim('AnonForm'))
def AnonGoSim(self, name):
_translate = QtCore.QCoreApplication.translate
AnonForm.setWindowTitle(_translate(name, "Change Me - Anoujaa SAS"))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.AnonGroupsLui = QtWidgets.QLineEdit(AnonForm)
self.AnonGroupsLui.setGeometry(QtCore.QRect(40, 60, 80, 40))
self.AnonGroupsLui.setFont(font)
self.AnonGroupsLui.setMaxLength(6)
self.AnonGroupsLui.setAlignment(QtCore.Qt.AlignCenter)
self.AnonGroupsLui.setObjectName("AnonGroupsLui")
def AnonOnpercentInputChg(self, text):
if text:
self.AnonGo.setEnabled(True)
else:
self.AnonGo.setEnabled(False)
def retranslateUi(self, AnonForm):
_translate = QtCore.QCoreApplication.translate
AnonForm.setWindowTitle(_translate("AnonForm", "King - Anoujaa SAS"))
self.AnonTxt1.setText(_translate("AnonForm", "Grupos"))
self.AnonGo.setText(_translate("AnonForm", "Go!"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
AnonForm = QtWidgets.QWidget()
ui = Ui_AnonForm()
ui.setupUi(AnonForm)
AnonForm.show()
sys.exit(app.exec_())
私の意見では、QHboxLayoutを使用して、新しく作成されたすべてのウィジェットをaddRowを使って追加する必要があります。 – Ashish
しかし、ウィジェットを追加したくないQPushButtonを押すと、同じウィジェットにさらに多くのQLineEditsを追加したいだけです。それをどうやって解決できますか? – 0st14H4ck