2016-04-14 13 views
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_()) 
+0

私の意見では、QHboxLayoutを使用して、新しく作成されたすべてのウィジェットをaddRowを使って追加する必要があります。 – Ashish

+0

しかし、ウィジェットを追加したくないQPushButtonを押すと、同じウィジェットにさらに多くのQLineEditsを追加したいだけです。それをどうやって解決できますか? – 0st14H4ck

答えて

0

は、私はあなたがやろうとしている理解何の粗製の実装です。 QLineEditを追加できるレイアウトを追加する必要があります。 QLineEditもウィジェットです

from PyQt5.QtWidgets import * 

class Form(QMainWindow): 

x = 1 
def __init__(self): 
    super(Form, self).__init__() 

    self.setUI() 

def setUI(self): 
    self.lineEdit = QLineEdit() 
    self.pushButton = QPushButton("Go") 

    self.layout = QGridLayout() 
    self.layout.addWidget(self.lineEdit,0,0) 
    self.layout.addWidget(self.pushButton,0,1) 

    self.main_widget = QWidget() 
    self.main_widget.setLayout(self.layout) 
    self.setCentralWidget(self.main_widget) 

    self.pushButton.clicked.connect(self.addLineEdit) 

def addLineEdit(self): 

    newLineEdit = QLineEdit() 
    self.layout.addWidget(newLineEdit,Form.x,0) 
    Form.x += 1 

if __name__ == "__main__": 
    import sys 
    app = QApplication(sys.argv) 
    form = Form() 
    form.show() 
    sys.exit(app.exec_()) 
関連する問題