【问题标题】:PyQt change picture with keyboard buttonPyQt用键盘按钮改变图片
【发布时间】:2018-03-11 14:42:25
【问题描述】:

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

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 确实发生了变化,当我一开始停止显示时,它调整了窗口大小,但仍然没有显示图像。关于我做错了什么有什么建议吗?

这是一个更大项目的一部分,这是图片侧面额外空间的原因,但我们将不胜感激。

【问题讨论】:

    标签: python-3.x pyqt pyqt5


    【解决方案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()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      相关资源
      最近更新 更多