Answer the question
In order to leave comments, you need to log in
Why does the picture in PyQt5 not open correctly?
Here is the code:
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QLineEdit, QPushButton
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QPixmap
from PIL import Image
class OpenImage(QWidget):
def __init__(self):
super().__init__()
x, y = 800, 600
self.setGeometry(0, 0, x, y)
self.pushOK = QPushButton("Open", self)
self.pushOK.resize(QSize(100, 20))
self.pushOK.move(220, 20)
self.pushOK.clicked.connect(self.init_UI)
self.nameInput = QLineEdit(self)
self.nameInput.move(40, 20)
self.nameInput.resize(QSize(175, 20))
self.lbl1 = QLabel(self)
def init_UI(self):
try:
screensize = (800, 600)
picname = self.nameInput.text()
x, y = Image.open(picname).size
self.setGeometry(0, 0, x, y)
self.setWindowFilePath(picname)
self.lbl1.resize(QSize(x, y))
pixmap = QPixmap(picname)
k1, k2 = 1, 1
if not (pixmap.width() < screensize[0] and pixmap.height() < screensize[1]):
try:
k1 = pixmap.height() // screensize[0]
k2 = pixmap.width() // screensize[1]
except ZeroDivisionError:
k1, k2 = 1, 1
print(k1, k2)
pixmap = pixmap.scaled(pixmap.width() // max(k1, k2),
pixmap.height() // max(k1, k2))
self.lbl1.setPixmap(pixmap)
self.lbl1.move(0, 40)
self.move(0, 0)
except FileNotFoundError:
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = OpenImage()
ex.show()
sys.exit(app.exec())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question