如何使用PyQT5中的按钮单击在单独的窗口中打开图像?

时间:2019-12-03 18:39:07

标签: python pyqt5

from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QFileDialog, QPushButton, QLineEdit,QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QDialog, QVBoxLayout
import Updated_encrypt
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.title = 'Encrypt/Decrypt'
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300

        self.InitWindow()

    def InitWindow(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        vbox = QVBoxLayout()

        self.button1 = QPushButton('Encrypt')
        self.button1.clicked.connect(self.openSecondDialog)

        self.button2 = QPushButton('Decrypt')

        vbox.addWidget(self.button1)
        vbox.addWidget(self.button2)

        self.setLayout(vbox)

        self.show()

    def openSecondDialog(self):
        hbox = QVBoxLayout()
        mydialog = QDialog(self)
        mydialog.show()

        self.button2 = QPushButton('Check Image')
        self.button2.clicked.connect(self.getImage)
        hbox.addWidget(self.button2)
        self.setLayout(hbox)
        self.show()

    def getImage(self):
         hbox = QHBoxLayout()
         file_Name = QFileDialog.getOpenFileName(self,
                                                'OpenFile',
                                                '',
                                                '')
         image_path = file_Name[0]
         updatedImage = Updated_encrypt.decrypt(image_path, 123)
         pixmap = QPixmap(updatedImage)
         self.label.setPixmap(QPixmap(pixmap))
         self.resize(pixmap.width(), pixmap.height())

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

我设置了代码,以实现从弹出对话框中选择图像时修改图像的算法。我要完成的工作是,单击“加密”按钮后,图像会在单独的窗口中弹出。除了窗口本身之外,我似乎无法在其他窗口中弹出任何内容。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您至少存在以下错误:

  • 已将创建的“ hbox”添加到窗口而不是QDialog:self.setLayout(hbox),它必须为mydialog.setLayout(hbox)

  • 请勿对两个不同的对象使用相同的名称,因为它们可能会引起问题,在您的情况下,有两个QPushButton分配给变量“ self.button2”。

  • 您尝试使用变量“ self.label”,但请不要相信它。

考虑到上述情况,我们可以进行以下改进:

  • 使用更多描述性名称来轻松区分其功能。

  • 如果要创建一个目标不同的窗口,最好创建一个类。

以上考虑到上述解决方案,避免了指示的问题:

import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (
    QApplication,
    QDialog,
    QFileDialog,
    QHBoxLayout,
    QLabel,
    QPushButton,
    QVBoxLayout,
    QWidget,
)

import Updated_encrypt


class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.load_image_btn = QPushButton("Check Image")
        self.load_image_btn.clicked.connect(self.load_image)

        self.image_lbl = QLabel()
        lay = QVBoxLayout(self)
        lay.addWidget(self.load_image_btn)
        lay.addWidget(self.image_lbl)

    def load_image(self):
        image_path, _ = QFileDialog.getOpenFileName(self, "OpenFile", "", "")
        if image_path:
            updatedImage = Updated_encrypt.decrypt(image_path, 123)
            pixmap = QPixmap(updatedImage)
            self.image_lbl.setPixmap(QPixmap(pixmap))


class Window(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.title = "Encrypt/Decrypt"
        self.InitWindow()

    def InitWindow(self):
        self.setWindowTitle(self.title)
        self.setGeometry(200, 500, 400, 300)

        self.encrypt_btn = QPushButton("Encrypt")
        self.encrypt_btn.clicked.connect(self.openSecondDialog)

        self.decrypt_btn = QPushButton("Decrypt")

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.encrypt_btn)
        vbox.addWidget(self.decrypt_btn)

    def openSecondDialog(self):

        dialog = Dialog(self)
        dialog.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())