【问题标题】:Open and save image file using menubar in PyQt5在 PyQt5 中使用菜单栏打开和保存图像文件
【发布时间】:2018-10-23 02:46:39
【问题描述】:

我编写了以下代码来使用 PyQt5 中的菜单栏打开图像文件。它能够选择文件但不能在窗口中显示它。我已成功打开文本文件,但无法对图像执行相同操作。你能纠正我的错误吗?

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

【问题讨论】:

    标签: python pyqt pyqt5 qmainwindow qlabel


    【解决方案1】:

    当您调用 show() 时,小部件使子级可见,在您的情况下,使用该方法时 QLabel 不是子级,因此一个简单但部分的解决方案是使其可见:

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

    但在 QMainWindow 不适合的情况下,QMainWindow 是一个非常特殊的小部件,因为它具有如下图所示的明确结构:

    如您所见,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()) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      相关资源
      最近更新 更多