Issue
I'm trying to write an image display function that instantiates the following class and renders the image when I double-click the image, I wanted to close the window by pressing ESC, but it didn't seem to work. The keyPressEvent function was written before copying. It worked before, but it didn't work here. I'm sad.
class ImageLabel(QtGui.QWidget):
def __init__(self, imagePath, parent=None):
super(ImageLabel, self).__init__(parent)
self.imagePath = imagePath
self.initUI()
def initUI(self):
from PIL import Image
pic_size = (Image.open(self.imagePath).width, Image.open(self.imagePath).height)
self.image_Label = QtGui.QLabel()
self.image_Label.setWindowIcon(QtGui.QIcon(fileconfig.MAIN_ICON))
self.image_Label.resize(pic_size[0] + 50, pic_size[1] + 50)
self.image_Label.setWindowTitle(os.path.basename(self.imagePath))
self.image_Label.setWindowModality(QtCore.Qt.ApplicationModal)
self.image_Label.setPixmap(QtGui.QPixmap(self.imagePath))
self.image_Label.setAlignment(QtCore.Qt.AlignCenter)
self.image_Label.show()
def keyPressEvent(self, event):
key = event.key()
if key == QtCore.Qt.Key_Escape:
self.close()
Solution
Here is working code:
from PySide import QtGui,QtCore
import sys
import os
class ImageLabel(QtGui.QWidget):
def __init__(self, imagePath, parent=None):
super(ImageLabel, self).__init__(parent)
self.imagePath = imagePath
self.initUI()
def initUI(self):
from PIL import Image
pic_size = (Image.open(self.imagePath).width,
Image.open(self.imagePath).height)
self.image_Label = QtGui.QLabel()
self.image_Label.setWindowIcon(QtGui.QIcon(fileconfig.MAIN_ICON))
self.image_Label.resize(pic_size[0] + 50, pic_size[1] + 50)
self.image_Label.setWindowTitle(os.path.basename(self.imagePath))
self.image_Label.setWindowModality(QtCore.Qt.ApplicationModal)
self.image_Label.setPixmap(QtGui.QPixmap(self.imagePath))
self.image_Label.setAlignment(QtCore.Qt.AlignCenter)
# self.image_Label.show()
def keyPressEvent(self, event):
key = event.key()
if key == QtCore.Qt.Key_Escape:
self.close()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
il = ImageLabel('usr/image/img.png')
il.show()
sys.exit(app.exec_())
I just commented one line self.image_Label.show()
and everything works fine for me
Answered By - prafull shinde
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.