作为子窗口小部件反复调用时,QLabel上的setPixmap不执行任何操作

时间:2019-06-11 15:52:18

标签: python pyqt pyqt5 python-3.7 qlabel

以下两个类非常相似,但是它们的行为并不相同。第二种方式很明显:每单击一次按钮,幻灯片中都会显示一个新图像。但是第一个没有。相反,该行为很奇怪。首次单击“下一步”按钮时,将显示一个新图像。除非您移动或调整窗口大小,否则随后的按钮单击不会发生。有人可以向我解释为什么吗?

class BrokenSlideWidget(QWidget):
    def __init__(self, images):
        super().__init__()

        # The code that is different between two
        # #######################################
        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        # #######################################

        self.pixmaps = [QPixmap(img) for img in images]
        self.index = 0
        self.page_label = QLabel()
        self.page_label.setPixmap(self.pixmaps[self.index])
        self.next_button = QPushButton(">")
        self.next_button.clicked.connect(self.next)
        self.previous_button = QPushButton("<")
        self.previous_button.clicked.connect(self.previous)
        self.layout.addWidget(self.previous_button)
        self.layout.addWidget(self.page_label)
        self.layout.addWidget(self.next_button)
        self.show()

    def previous(self):
        if self.index > 0:
            self.index = self.index - 1
        self.page_label.setPixmap(self.pixmaps[self.index])

    def next(self):
        self.index = self.index + 1
        try:
            self.page_label.setPixmap(self.pixmaps[self.index])
        except IndexError:
            # Do handling

class WorkingSlideWidget(QWidget):
    def __init__(self, images):
        super().__init__()

        # The code that is different between the two
        # ##########################################
        self.master_layout = QVBoxLayout()
        self.layout = QHBoxLayout()
        self.master_layout.addLayout(self.layout)
        self.setLayout(self.master_layout)
        # ##########################################

        self.pixmaps = [QPixmap(img) for img in images]
        self.index = 0
        self.page_label = QLabel()
        self.page_label.setPixmap(self.pixmaps[self.index])
        self.next_button = QPushButton(">")
        self.next_button.clicked.connect(self.next)
        self.previous_button = QPushButton("<")
        self.previous_button.clicked.connect(self.previous)
        self.layout.addWidget(self.previous_button)
        self.layout.addWidget(self.page_label)
        self.layout.addWidget(self.next_button)
        self.show()

    def previous(self):
        if self.index > 0:
            self.index = self.index - 1
        self.page_label.setPixmap(self.pixmaps[self.index])

    def next(self):
        self.index = self.index + 1
        try:
            self.page_label.setPixmap(self.pixmaps[self.index])
        except IndexError:
            # Do handling

第二堂课有效。第一个坏了。发生了什么事?

0 个答案:

没有答案