简体   繁体   English

Python PyQt4如何使用QFileDialog打开图像

[英]Python PyQt4 how to open image using QFileDialog

I have to write a program with an option to open an image from a file. 我必须编写一个带有从文件打开图像选项的程序。 I have to use QFileDialog and display image in QLabel , using QPixmap . 我必须使用QFileDialog并使用QPixmapQLabel显示图像。 I am able to use them individually but I didn't manage to combine them. 我可以单独使用它们,但无法合并使用。 I think I need to take my image name from dlg.selectedFiles but I don't know how to choose the moment when there is useful data in it. 我想我需要从dlg.selectedFiles获取图像名称,但是我不知道如何选择其中包含有用数据的时刻。 Do I need to make a loop in my main program, and constantly check if there is image to open? 我是否需要在主程序中进行循环,并不断检查是否有要打开的图像? Can I send a signal to my label using openAction.triggered.connect(...) ? 我可以使用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()) 

You need to make your own slot and connect it to the openAction signal. 您需要创建自己的插槽并将其连接到openAction信号。
In your __init__ function do: __init__函数中执行以下操作:

openAction.triggered.connect(self.openSlot)  

In your class MainWindow define the following function: 在您的类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.

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

相关问题 Python PyQt4 QFileDialog图像并在QListWidget中加载 - Python PyQt4 QFileDialog images and load in QListWidget PyQt4:使用QFileDialog()保存文件而不是QFileDialog()。getSaveFileName() - PyQt4: Using QFileDialog() to save files not QFileDialog().getSaveFileName() 在qtableview pyqt4 python中使用图像的委托? - using delegate for image in qtableview pyqt4 python? 如何使用 pyqt4 创建圆形图像? - How to create circular image using pyqt4? Python PyQt5:如何通过QFileDialog获取图像文件的名称和内容 - Python PyQt5: How to get the name and the content of an image file by QFileDialog PIL如何使用pyqt4资源文件打开图像? - can PIL open an image using pyqt4 resource file? 如何使用pyqt QFileDialog将图像上传到sqllite数据库? - how do i upload an image to sqllite database using pyqt QFileDialog? 如何使用python在Maya中设置目录? 目前正在使用PyQt4,但欢迎提出任何建议 - How do you set the directory in Maya with python? Currently using PyQt4 but open to any suggestions 使用PyQt4库使用python应用程序构建docker映像 - Build docker image with python application using PyQt4 library 如何在特定的需要点使用 Python 将图像添加到 QTextBrowser (PyQt4)? - How to add image to QTextBrowser (PyQt4) using Python at a specific required point?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM