PyQt5:使用按钮动态添加复选框

时间:2018-07-17 13:34:29

标签: python-3.x pyqt5

在PyQt5中,每次单击按钮时如何添加复选框?

每次单击按钮时,它都会生成一个复选框。

我正在使用的主要行:

class Sample extends Component {
  state = {
    text: sample('igod', 1),
  };

  componentDidMount() {
    setTimeout(() => {
      this.setState({
        text: this.sample('igod',1)
      });
    }, 1000);
  }

  render() {
    return (
      <div className="text-intro" id="site-type">
        {this.state.text}
      </div>
    );
  }
}

我不知道该怎么做!

1 个答案:

答案 0 :(得分:1)

尝试一下:

from PyQt5 import QtWidgets, QtGui, QtCore

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.vlayout = QtWidgets.QVBoxLayout()
        self.pushButton_ok = QtWidgets.QPushButton("Press me", self)
        self.pushButton_ok.clicked.connect(self.addCheckbox)
        self.vlayout.addWidget(self.pushButton_ok)

        self.checkBox = QtWidgets.QCheckBox(self)
        self.vlayout.addWidget(self.checkBox)
        self.setLayout(self.vlayout)

    def addCheckbox(self):
        #checkBox = QtWidgets.QCheckBox()
        self.vlayout.addWidget(QtWidgets.QCheckBox()) 

application = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Dynamically adding checkboxes using a push button')
window.resize(250, 180)
window.show()
sys.exit(application.exec_())        

enter image description here

相关问题