2017-09-29 17 views
0

これは私の最初の投稿です。どこでも見たことがないのでうまくいけばOKです。私はキーボードのクリックで表示された画像を変更しようとしています(スライドショーを考えてください)。キーボードのボタンでPyQtの画像を変更する

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が変更を行い知っている、と私は最初に表示するのを停止したときに、それは、ウィンドウをリサイズ:これは、これまでの私のコードですそれでもイメージは表示されませんでした。私が間違っていることに関する提案はありますか?

これは画像の横に余分なスペースが必要な大きなプロジェクトの一部ですが、助けていただければ幸いです。

答えて

0

もうすぐです。以下を試してください...

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()を1回だけ呼び出す必要があります。

関連する問題