【问题标题】:Python PyQt4 how to open image using QFileDialogPython PyQt4如何使用QFileDialog打开图像
【发布时间】:2015-11-30 01:58:47
【问题描述】:

我必须编写一个带有从文件中打开图像的选项的程序。我必须使用QFileDialog 并在QLabel 中显示图像,使用QPixmap。我可以单独使用它们,但我没有设法将它们结合起来。 我想我需要从dlg.selectedFiles 中获取我的图像名称,但我不知道如何选择其中有有用数据的时刻。我是否需要在我的主程序中创建一个循环,并不断检查是否有图像要打开?我可以使用openAction.triggered.connect(...) 向我的标签发送信号吗?

from PyQt4 import QtGui
import sys

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')
        dlg = QtGui.QFileDialog(self)       
        openAction = QtGui.QAction('Open', self)  
        openAction.triggered.connect(dlg.open)     
        fileMenu.addAction(openAction)
        #label = QtGui.QLabel(self)
        #pixmap = QtGui.QPixmap('')
        #label.setPixmap(pixmap)

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

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

【问题讨论】:

    标签: python pyqt4 qfiledialog


    【解决方案1】:

    您需要制作自己的插槽并将其连接到openAction 信号。
    在您的__init__ 函数中执行:

    openAction.triggered.connect(self.openSlot)  
    

    在您的班级MainWindow 中定义以下函数:

    def openSlot(self):
        # This function is called when the user clicks File->Open.
        filename = QtGui.QFileDialog.getOpenFileName()
        print(filename)
        # Do your pixmap stuff here.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多