PyQt5 - 当我按下按钮时,无法向表单添加更多输入

时间:2016-04-14 05:11:02

标签: python pyqt5

我在PyQt5中有一个表单,我添加了一些QLineEdits字段。

我想在用户点击QLineEdits时以编程方式将QPushButton字段添加到该表单,但是当我运行我的应用并点击按钮时,我的应用不会添加任何内容表格,我没有任何错误。

那么,我该怎么做才能解决这个问题呢? 这是我的代码:

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_())

2 个答案:

答案 0 :(得分: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_())

答案 1 :(得分:0)

每次单击按钮时,您需要创建一个新的行编辑和标签,然后为其分配唯一的对象名

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Sample Dynamic LineEdit'
        self.left = 10
        self.top = 10
        self.width = 400
        self.height = 1000
        self.i = 40
        self.j = 80
        self.counter = 1
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox
        # self.textbox = QLineEdit(self)
        # self.textbox.move(20, 20)
        # self.textbox.resize(280, 40)

        # Create a button in the window
        self.button = QPushButton('Add Line Edit', self)

        # connect button to function on_click
        self.button.clicked.connect(self.on_click)
        self.show()

    @pyqtSlot()
    def on_click(self):
        #this creates a new field and label everytime the button is clicked
        self.textbox = QLineEdit(self)
        self.label = QLabel(self)
        self.label.setText(str(self.counter))
        self.label.move(5, self.i)
        self.button.move(20, self.j)
        self.textbox.move(20, self.i)
        self.textbox.resize(280, 40)
        #dynamic object names
        self.textbox.setObjectName("text" + str(self.counter))
        self.textbox.show()
        self.label.show()
        self.i += 40
        self.j += 40
        self.counter += 1
        print(self.textbox.objectName())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())