在PyQt5中按下按钮时如何插入新的布局小部件

时间:2019-01-05 18:03:14

标签: python pyqt5

当我单击“ self.normal_replace_btn”按钮时如何插入新的行窗口小部件布局?

import pandas as pd
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Form(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.normal_replace_btn = QPushButton('Add normal replace function')

        self.Add_btn_layout = QHBoxLayout()
        self.Add_btn_layout.addWidget(self.normal_replace_btn)

        self.normal_replace_btn.clicked.connect(self.insert_normal_textbox)

        self.confirm_btn = QPushButton('Confirm')
        self.button_layout = QHBoxLayout()
        self.button_layout.addWidget(self.confirm_btn, alignment=Qt.AlignRight)

        self.layout = QVBoxLayout()
        self.layout.addLayout(self.Add_btn_layout)

        self.layout.addLayout(self.insert_normal_textbox())
        self.layout.addLayout(self.button_layout)
        self.setLayout(self.layout)

        self.confirm_btn.clicked.connect(self.filter_data)

    def insert_normal_textbox(self):
        self.from_textBox = QLineEdit()
        row_1 = QLabel('To: ')
        self.to_textBox = QLineEdit()

        self.grid = QGridLayout()
        self.grid.addWidget(self.from_textBox, 0, 0)
        self.grid.addWidget(row_1, 0, 1)
        self.grid.addWidget(self.to_textBox, 0, 2)


app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())

下面的图像用于解释有关我的问题和结果的更多细节

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试一下:

import pandas as pd
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Form(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.normal_replace_btn = QPushButton('Add normal replace function')

        self.Add_btn_layout = QHBoxLayout()
        self.Add_btn_layout.addWidget(self.normal_replace_btn)

        self.normal_replace_btn.clicked.connect(self.insert_normal_textbox)

        self.confirm_btn = QPushButton('Confirm')
        self.button_layout = QHBoxLayout()
        self.button_layout.addWidget(self.confirm_btn, alignment=Qt.AlignRight)

        self.layout = QVBoxLayout()
        self.layout.addLayout(self.Add_btn_layout)

#        self.layout.addLayout(self.insert_normal_textbox())  # ---
        self.layout.addLayout(self.button_layout)
        self.setLayout(self.layout)

        self.confirm_btn.clicked.connect(self.filter_data)

    def insert_normal_textbox(self):
        self.from_textBox = QLineEdit()
        row_1 = QLabel('To: ')
        self.to_textBox = QLineEdit()

        self.grid = QGridLayout()
        self.grid.addWidget(self.from_textBox, 0, 0)
        self.grid.addWidget(row_1, 0, 1)
        self.grid.addWidget(self.to_textBox, 0, 2)

        self.layout.addLayout(self.grid)                  # +++

    def filter_data(self):                                # +++
        print("def filter_data(self):")


app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())

enter image description here


示例二:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *

class Form(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.normal_replace_btn = QPushButton('Add normal replace function')

        self.Add_btn_layout = QHBoxLayout()
        self.Add_btn_layout.addWidget(self.normal_replace_btn)

        self.normal_replace_btn.clicked.connect(self.insert_normal_textbox)

        self.confirm_btn = QPushButton('Confirm')
        self.button_layout = QHBoxLayout()
        self.button_layout.addWidget(self.confirm_btn, alignment=Qt.AlignRight)

        self.layout = QVBoxLayout()
        self.layout.addLayout(self.Add_btn_layout)

        self.layout.addLayout(self.button_layout)
        self.setLayout(self.layout)

        self.confirm_btn.clicked.connect(self.filter_data)

    def insert_normal_textbox(self):
        self.from_textBox = QLineEdit() 
        row_1 = QLabel('To: ')
        self.to_textBox = QLineEdit()   

        self.grid = QGridLayout()
        self.grid.addWidget(self.from_textBox, 0, 0)
        self.grid.addWidget(row_1, 0, 1)
        self.grid.addWidget(self.to_textBox, 0, 2)
        self.layout.addLayout(self.grid)                  

        self.printLineEdit()                                        # +++

    def printLineEdit(self):                                        # +++ 
        print("\nlayoutСount: {}\n".format(self.layout.count()))
        layoutСhildrens = self.layout.children()
        print("layoutСhildrens: {}\n".format(layoutСhildrens))

        for i, child in enumerate(layoutСhildrens[2:]):
            print(" line {}: --> {} ; {} ; {}"
                  "".format(i+1, 
                            child.itemAt(0).widget().text(),
                            child.itemAt(1).widget().text(),
                            child.itemAt(2).widget().text()
                           )
                  )

    def filter_data(self):                                
        print("\ndef filter_data(self):\n")


app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())

enter image description here