Saya ingin membuat jendela sekunder untuk menampilkan gambar berukuran penuh, jadi itulah yang saya buat tetapi saya tidak dapat melihat gambarnya. Saya tidak tahu apakah itu sebabnya saya tidak membuka dengan benar atau apa. Saya sangat baru di Qt, jadi terbukti ini kesalahan yang sangat sederhana.

Terima kasih

Berikut kodenya:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

Ini adalah jendela utama, memiliki dialog file terbuka untuk memilih gambar, setelah itu menempatkan gambar dalam label. Ketika Anda mengklik tombol "Ampliar" Anda membuka jendela kedua dengan harus menampilkan gambar dengan ukuran aslinya.

# 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"))

Ini adalah kelas sekunder, di mana saya ingin meletakkan gambar yang saya buka di jendela lain, itu menerima jalurnya jadi saya pikir itu harus mudah

# 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"))

Ini yang utama

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
Roger Orriols i Valls 14 Agustus 2019, 11:10

1 menjawab

Jawaban Terbaik

Cobalah:

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_())        

enter image description here

0
S. Nick 14 Agustus 2019, 09:37