PyQt用键盘按钮改变图片

时间:2017-09-29 16:23:44

标签: python-3.x pyqt pyqt5

这是我在这里发表的第一篇文章,我没有在任何地方看到它,所以希望它没问题。我试图通过键盘点击更改显示的图像(想想幻灯片)。到目前为止,这是我的代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import os

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Document Analysis'
        self.left = 30
        self.top = 30
        self.width = 640
        self.height = 480
        self.imagenumber=0
        self.initUI()

    def keyPressEvent(self, event):
        key=event.key()
        if key==Qt.Key_Right:
            self.imagenumber=self.imagenumber+1
            self.showimage(self.imagenumber)
            self.show()
        else:
            super(self).keyPressEvent(event)

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.showimage(0)
        self.show()

    def showimage(self,imagenumber):
        label = QLabel(self)
        directory = "C:\\Desktop\\Pictures"
        imagelist = os.listdir(directory)
        pixmap = QPixmap(directory + '\\' + imagelist[imagenumber])
        label.setPixmap(pixmap)
        self.resize(pixmap.width() + 500, pixmap.height())
        self.show()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

似乎有点像某个地方,第一个图像显示得很好,我知道imagenumber确实发生了变化,当我停止显示它时,它会调整窗口大小,但仍然没有显示图像。关于我做错了什么的建议?

这是一个较大项目的一部分,这是图片侧面有额外空间的原因,但是会有所帮助。

1 个答案:

答案 0 :(得分:1)

你关闭了。请尝试以下方法......

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import os

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Document Analysis'
        self.left = 30
        self.top = 30
        self.width = 640
        self.height = 480
        self.imagenumber=0
        self.initUI()

    def keyPressEvent(self, event):
        key=event.key()
        if key==Qt.Key_Right:
            self.imagenumber=self.imagenumber+1
            self.showimage(self.imagenumber)
            # self.show()
        else:
            super(self).keyPressEvent(event)

    def initUI(self):
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.label = QLabel(self)
        layout.addWidget(self.label)

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.showimage(0)
        self.show()

    def showimage(self,imagenumber):
        # label = QLabel(self)

        directory = "C:\\Desktop\\Pictures"
        imagelist = os.listdir(directory)
        pixmap = QPixmap(directory + '\\' + imagelist[imagenumber])

        # label.setPixmap(pixmap)
        self.label.setPixmap(pixmap)
        self.resize(pixmap.width() + 500, pixmap.height())
        # self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

主要是,你需要一个持久的标签。您也只需要调用一次show()。