简体   繁体   English

使用PyQt5中的菜单栏打开并保存图像文件

[英]Open and save image file using menubar in PyQt5

I've written the following code to open image file using a menubar in PyQt5. 我编写了以下代码,使用PyQt5中的菜单栏打开图像文件。 It is able to select the file but not able to display it in the window. 它能够选择文件,但不能在窗口中显示它。 I've successfully opened text file but not able to do the same for images. 我已成功打开文本文件,但无法对图像执行相同操作。 Can you please rectify my error? 您能纠正我的错误吗?

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QFileDialog, QAction
from PyQt5.QtGui import QIcon, QPixmap

class MainWindow(QMainWindow):

    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        editMenu = menubar.addMenu('Edit')
        self.resize(500, 500)

        dlg = QFileDialog(self)       
        openAction = QAction('Open Image', self)  
        openAction.triggered.connect(self.openImage) 
        fileMenu.addAction(openAction)

        closeAction = QAction('Exit', self)  
        closeAction.triggered.connect(self.close) 
        fileMenu.addAction(closeAction)



    def openImage(self):
    # This function is called when the user clicks File->Open Image.
        label = QLabel(self)
        filename = QFileDialog.getOpenFileName()
        imagePath = filename[0]
        print(imagePath)
        pixmap = QPixmap(imagePath)
        label.setPixmap(pixmap)
        self.resize(pixmap.width(),pixmap.height())
        self.show()



def main():
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    app.exec_()

if __name__ == '__main__':
    sys.exit(main()) 

When you call show() the widget makes children visible, in your case QLabel is not a child when that method is used, so a trivial but partial solution is to make it visible: 当您调用show() ,小部件使子对象可见,在您的情况下,使用该方法时QLabel不是子对象,因此一个简单但部分的解决方案是使其可见:

def openImage(self):
    label = QLabel(self)
    label.show()
    # or
    # self.show()

But in the case of QMainWindow is not suitable, QMainWindow is a very special widgets because it has a definite structure as shown in the following image: 但是在不适合使用QMainWindow的情况下,QMainWindow是一个非常特殊的小部件,因为它具有确定的结构,如下图所示:

在此处输入图片说明

As you can see, QLabel is the centralWidget and create it only once, and then you only have to replace the QPixmap if you select a new image: 如您所见,QLabel是centralWidget,仅创建一次,然后,如果选择新图像,则只需替换QPixmap:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QFileDialog, QAction
from PyQt5.QtGui import QPixmap

class MainWindow(QMainWindow):

    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        editMenu = menubar.addMenu('Edit')
        self.resize(500, 500)

        openAction = QAction('Open Image', self)  
        openAction.triggered.connect(self.openImage) 
        fileMenu.addAction(openAction)

        closeAction = QAction('Exit', self)  
        closeAction.triggered.connect(self.close) 
        fileMenu.addAction(closeAction)
        self.label = QLabel()
        self.setCentralWidget(self.label)

    def openImage(self):
        imagePath, _ = QFileDialog.getOpenFileName()
        pixmap = QPixmap(imagePath)
        self.label.setPixmap(pixmap)
        self.resize(pixmap.size())
        self.adjustSize()

def main():
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    return app.exec_()

if __name__ == '__main__':
    sys.exit(main()) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM