我想创建一个辅助窗口来显示完整的图像,所以这就是我所做的,但看不到图像。我不知道这是为什么我无法正确打开的原因或原因。我对Qt还是很陌生,可以证明这是一个非常简单的错误。
谢谢
代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
这是主窗口,它具有一个打开的文件对话框,用于选择图像,一旦将其放入标签中即可。当您点击“ Ampliar”按钮时,您将打开第二个窗口,其中应显示原始大小的图像。
# Classe per la finestra principal de l'App
class Ui_MainWindow(object):
# Inicialitzacio de variables
def __init__(self):
self.fileName = None
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1080, 720)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Label on coloquem la nostra imatge
self.lMarcIm = QtWidgets.QLabel(self.centralwidget)
self.lMarcIm.setGeometry(QtCore.QRect(10, 10, 500, 500))
self.lMarcIm.setAutoFillBackground(False)
self.lMarcIm.setScaledContents(True)
self.lMarcIm.setObjectName("lMarcIm")
# Boto per carregar una imatge al programa
self.bObrirIm = QtWidgets.QPushButton(self.centralwidget)
self.bObrirIm.setGeometry(QtCore.QRect(10, 530, 111, 31))
self.bObrirIm.setObjectName("bObrirIm")
self.bObrirIm.clicked.connect(self.obrirDialegImatge)
# Boto per ampliar la imatge carregada
self.bAmpliarIm = QtWidgets.QPushButton(self.centralwidget)
self.bAmpliarIm.setGeometry(QtCore.QRect(130, 530, 121, 31))
self.bAmpliarIm.setObjectName("bAmpliarIm")
self.bAmpliarIm.clicked.connect(self.ampliarImatge)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
################################################################
# Bloc de Funcions #
################################################################
def obrirDialegImatge(self):
# Obrim la finestra de dialeg i obtenim el nom
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"QFileDialog.getOpenFileName()",
"",
"All Files (*);;Python Files (*.py)",
options=options)
# Posem l'imatge on toca del Layout
self.lMarcIm.setPixmap(QtGui.QPixmap(self.fileName))
def ampliarImatge(self):
#Comprovem que hi ha una imatge seleccionada
if self.fileName != None:
print("Imatge en gran")
self.openSecondWindow()
else:
self.popup_errorAmpliar()
def openSecondWindow(self):
self.secondWindow = QtWidgets.QMainWindow()
self.ui = Ui_SecondaryWindow()
self.ui.setupUi(self.secondWindow, self.fileName)
self.secondWindow.show()
def popup_errorAmpliar(self):
msg = QMessageBox()
msg.setWindowTitle("Alerta!")
msg.setText("No hi ha cap imatge carregada")
msg.setIcon(QMessageBox.Warning)
x = msg.exec_()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Image Processing APP"))
self.lMarcIm.setText(_translate("MainWindow", "TextLabel"))
self.bObrirIm.setText(_translate("MainWindow", "Obrir Imatge"))
self.bAmpliarIm.setText(_translate("MainWindow", "Ampliar"))
这是第二类,我想在另一个窗口中放置打开的图像,它会记录它的路径,因此我认为它必须很简单
# Classe per la finestra secundaria de l'App
class Ui_SecondaryWindow(object):
def setupUi(self, SecondaryWindow, im):
SecondaryWindow.setObjectName("SecondaryWindow")
SecondaryWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondaryWindow)
self.centralwidget.setObjectName("centralwidget")
self.lmidaOriginal = QtWidgets.QLabel(self.centralwidget)
self.lmidaOriginal.setGeometry(QtCore.QRect(150, 60, 251, 101))
self.lmidaOriginal.setObjectName("lmidaOriginal")
self.openImage(im)
SecondaryWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(SecondaryWindow)
QtCore.QMetaObject.connectSlotsByName(SecondaryWindow)
def openImage(self, imRoute):
print(imRoute)
pixmap = QtGui.QPixmap("imRoute")
self.lmidaOriginal.setPixmap(pixmap)
#self.lmidaOriginal.resize(pixmap.width(), pixmap.height())
def retranslateUi(self, SecondaryWindow):
_translate = QtCore.QCoreApplication.translate
SecondaryWindow.setWindowTitle(_translate("SecondaryWindow", "Apmliated Image"))
self.lmidaOriginal.setText(_translate("SecondaryWindow", "TextLabel"))
这是主要的
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
尝试一下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
# Classe per la finestra secundaria de l'App
class Ui_SecondaryWindow(object):
def setupUi(self, SecondaryWindow): #, im):
SecondaryWindow.setObjectName("SecondaryWindow")
# SecondaryWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondaryWindow)
self.centralwidget.setObjectName("centralwidget")
self.lmidaOriginal = QtWidgets.QLabel(self.centralwidget)
# self.lmidaOriginal.setGeometry(QtCore.QRect(150, 60, 251, 101))
self.lmidaOriginal.setObjectName("lmidaOriginal")
# self.openImage(im)
SecondaryWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(SecondaryWindow)
QtCore.QMetaObject.connectSlotsByName(SecondaryWindow)
"""
def openImage(self, imRoute):
print("imRoute---", imRoute)
pixmap = QtGui.QPixmap("imRoute")
pixmap = QtGui.QPixmap(imRoute) #("im.png") #(imRoute)
self.lmidaOriginal.setPixmap(pixmap)
"""
def retranslateUi(self, SecondaryWindow):
_translate = QtCore.QCoreApplication.translate
SecondaryWindow.setWindowTitle(_translate("SecondaryWindow", "Apmliated Image"))
self.lmidaOriginal.setText(_translate("SecondaryWindow", "TextLabel"))
# Classe per la finestra principal de l'App
class Ui_MainWindow(object):
# Inicialitzacio de variables
def __init__(self):
self.fileName = None
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1080, 720)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Label on coloquem la nostra imatge
self.lMarcIm = QtWidgets.QLabel(self.centralwidget)
self.lMarcIm.setGeometry(QtCore.QRect(10, 10, 500, 500))
self.lMarcIm.setAutoFillBackground(False)
self.lMarcIm.setScaledContents(True)
self.lMarcIm.setObjectName("lMarcIm")
# Boto per carregar una imatge al programa
self.bObrirIm = QtWidgets.QPushButton(self.centralwidget)
self.bObrirIm.setGeometry(QtCore.QRect(10, 530, 111, 31))
self.bObrirIm.setObjectName("bObrirIm")
self.bObrirIm.clicked.connect(self.obrirDialegImatge)
# Boto per ampliar la imatge carregada
self.bAmpliarIm = QtWidgets.QPushButton(self.centralwidget)
self.bAmpliarIm.setGeometry(QtCore.QRect(130, 530, 121, 31))
self.bAmpliarIm.setObjectName("bAmpliarIm")
self.bAmpliarIm.clicked.connect(self.ampliarImatge)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
################################################################
# Bloc de Funcions #
################################################################
def obrirDialegImatge(self):
# Obrim la finestra de dialeg i obtenim el nom
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"Select Image",
"",
"Image Files (*.png *.jpg *.jpeg *.bmp)", # +++
options=options)
# Posem l'imatge on toca del Layout
self.lMarcIm.setPixmap(QtGui.QPixmap(self.fileName))
def ampliarImatge(self):
#Comprovem que hi ha una imatge seleccionada
if self.fileName != None:
print("Imatge en gran")
self.openSecondWindow()
else:
self.popup_errorAmpliar()
def openSecondWindow(self):
self.secondWindow = QtWidgets.QMainWindow()
self.ui = Ui_SecondaryWindow()
# self.ui.setupUi(self.secondWindow, self.fileName)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.ui.setupUi(self.secondWindow)
self.ui.lmidaOriginal.setPixmap(QtGui.QPixmap(self.fileName))
self.ui.lmidaOriginal.adjustSize()
self.secondWindow.resize(self.ui.lmidaOriginal.width(), self.ui.lmidaOriginal.height())
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.secondWindow.show()
def popup_errorAmpliar(self):
msg = QMessageBox()
msg.setWindowTitle("Alerta!")
msg.setText("No hi ha cap imatge carregada")
msg.setIcon(QMessageBox.Warning)
x = msg.exec_()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Image Processing APP"))
self.lMarcIm.setText(_translate("MainWindow", "TextLabel"))
self.bObrirIm.setText(_translate("MainWindow", "Obrir Imatge"))
self.bAmpliarIm.setText(_translate("MainWindow", "Ampliar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())