PyQt使用键盘按钮更改图片

3

这是我在这里发表的第一篇帖子,我还没有看到类似的内容,所以希望没问题。我正在尝试通过键盘点击(类似于幻灯片)更改显示的图像。这是目前我的代码:

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_())

看起来进展不错,第一张图片正常显示,我知道图像编号确实发生了变化,但当我尝试停止它首次显示时,它会调整窗口大小但仍然不显示图像。您对我做错了什么有什么建议吗?

这是一个更大项目的一部分,这就是图片侧面有额外空间的原因,但是如果能得到帮助将不胜感激。

1个回答

3

你很接近了。请尝试以下方法...

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()方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接