首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何删除在PyQt5的另一个函数中添加的QPushButton?

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

Stack Overflow用户
提问于 2018-07-24 02:22:26
回答 1查看 631关注 0票数 -1

我正在试着写一个GUI应用程序,当你按下“清除”按钮时,它会添加一个“删除”按钮,而“清除”必须变成“添加”。“删除”按钮应该添加到QHBoxLayout中,然后当你按下“添加”时-它会从QHBoxLayout中删除“删除”按钮。我试图找到添加的按钮并对其使用deleteLater() --但它给了我一个运行时错误(我得到的类型是QWidget,而不是QPushButton)。

我已经尝试将项转换为QPushButton,然后使用deleteLater() -仍然不起作用。

欢迎任何帮助。

代码语言:javascript
复制
    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)
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51485073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档