如何删除在PyQt5的另一个函数中添加的QPushButton?

时间:2018-07-23 18:22:26

标签: python pyqt5

我正在尝试编写一个GUI应用程序,当您按下“清除”按钮时,该应用程序将添加“删除”按钮,并且“清除”必须变为“添加”。 “删除”按钮应添加到QHBoxLayout中,然后当您按“添加”时-它将从QHBoxLayout中删除“删除”按钮。 香港专业教育学院试图找到添加按钮ive并在其上使用deleteLater()-但这给了我一个运行时错误(我得到的类型是QWidget而不是QPushButton)。

我试图将项目投射到QPushButton,然后使用deleteLater()-仍然无法正常工作。

欢迎任何帮助。

    def add_image(self, button_index):
    try:
        file_path = QFileDialog.getOpenFileName(self, 'Open Image', '*.jpg')
    except FileNotFoundError:
        self.status_bar.showMessage('The path {} is not valid'.format(file_path))
    else:
        if file_path[0] != '':

            new_image=QPixmap(file_path[0]).scaled(self.image_height,self.image_width)

            self.image_label_list[button_index].setPixmap(new_image)
            self.button_list[button_index].setText('Remove')
            self.status_bar.showMessage(
                'Image {} was added successfully!'.format(get_name_from_path(file_path[0]))
            )

            if self.button_layout_list[button_index].count() > 1:
                delete_button = type(PyQt5.QtWidgets.QPushButton)(self.button_layout_list[button_index].itemAt(1))



                delete_button.deleteLater()

                self.button_layout_list[button_index].removeWidget(self.button_layout_list[button_index].itemAt(1))
                print('number of items: {}'.format(self.button_layout_list[button_index].count()))

            if (button_index - (len(self.button_list) - 1)) == 0:
                empty_image_layout = QVBoxLayout()
                button_box_layout = QHBoxLayout()

                empty_pic_label = QLabel()
                empty_pic = QPixmap('empty p.png').scaled(self.image_height, self.image_width)
                empty_pic_label.setPixmap(empty_pic)

                add_button = QPushButton('Add')
                # add_button.setFixedWidth(self.image_width)
                add_button.clicked.connect(self.on_click)
                button_box_layout.addWidget(add_button)

                empty_image_layout.addWidget(empty_pic_label)
                empty_image_layout.addLayout(button_box_layout)

                item = self.image_box.itemAt(self.stretch_index)

                self.image_box.removeItem(item)
                self.image_box.addLayout(empty_image_layout)
                self.image_box.addStretch()
                self.stretch_index += 1

                # Book keeping
                self.image_layout_list.append(empty_image_layout)
                self.button_layout_list.append(button_box_layout)
                self.image_label_list.append(empty_pic_label)
                self.button_list.append(add_button)

def remove_image(self, button_id, button_index):
    image_label = self.image_label_list[button_index]
    image_label.setPixmap(self.empty_pic)
    button_id.setText('Add')
    delete_button = QPushButton('Delete')
    print(type(delete_button))
    print(self.button_layout_list[button_index].count())
    self.button_layout_list[button_index].addWidget(delete_button)

1 个答案:

答案 0 :(得分:0)

尝试:

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

class Window(QWidget): 
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(500, 100, 300, 200)
        self.setWindowTitle("PushButton - `Clear` -> `Add` + `Delete`")
        self.setWindowIcon(QIcon('E:\\_Qt\\img\\qt-logo.png'))

        self.layout = QHBoxLayout()
        self.btn    = QPushButton("Clear")  
        self.btn.clicked.connect(self.buttonPress)
        self.layout.addWidget(self.btn)

        self.btnDelete = QPushButton("Delete")  
        self.btnDelete.clicked.connect(self.buttonDelete)

        self.setLayout(self.layout)
        self.show()

    def buttonPress(self):
        sender = self.sender()
        if sender.text() == "Clear":
            self.btn.setText("Add")
            self.layout.addWidget(self.btnDelete)
            self.btnDelete.show()
        elif sender.text() == "Add":
            print("sender - self.btn", sender.text())
            self.btnDelete.hide()
            self.btn.setText("Clear")

    def buttonDelete(self):
        print("You pressed the button `{}`".format(self.sender().text()))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    test = Window()
    #test.show()
    sys.exit(app.exec_())  

enter image description here