调用QtGui.QFileDialog.getExistingDirectory时出错

时间:2015-08-17 17:58:24

标签: python pyqt4 qfiledialog

在pyqt代码中,我试图向用户宣传一个对话框,让用户选择一个文件夹。似乎QtGui.QFileDialog.getExistingDirectory方法应该能够做到这一点。问题是代码运行后会出现一些错误消息。

D_Lib: debug printing for files [.*] and level [100] is turned on
D_Lib: debug printing for files [.*] and level [200] is turned on
D_Lib: debug printing for files [.*] and level [300] is turned on
11148:vf_shex.cpp(84): INFO: DllCanUnloadNow returned S_OK.

发生了什么事?

示例如下所示:

import sys
from PyQt4 import QtGui, QtCore

class FilePicker(QtGui.QWidget):
    """
    An example file picker application
    """

    def __init__(self):
        # create GUI
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('File picker')
        # Set the window dimensions
        self.resize(300,75)

        # vertical layout for widgets
        self.vbox = QtGui.QVBoxLayout()
        self.setLayout(self.vbox)

        # Create a label which displays the path to our chosen file
        self.lbl = QtGui.QLabel('No file selected')
        self.vbox.addWidget(self.lbl)

        # Create a push button labelled 'choose' and add it to our layout
        btn = QtGui.QPushButton('Choose file', self)
        self.vbox.addWidget(btn)

        # Connect the clicked signal to the get_fname handler
        self.connect(btn, QtCore.SIGNAL('clicked()'), self.get_fname)

    def get_fname(self):
        """
        Handler called when 'choose file' is clicked
        """
        # When you call getOpenFileName, a file picker dialog is created
        # and if the user selects a file, it's path is returned, and if not
        # (ie, the user cancels the operation) None is returned
        try:
            fname = QtGui.QFileDialog.getOpenFileName(self, 'Select file')
        except:
            pass
        if fname:
            self.lbl.setText(fname)
        else:
            self.lbl.setText('No file selected')


# If the program is run directly or passed as an argument to the python
# interpreter then create a FilePicker instance and show it
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = FilePicker()
    gui.show()
    app.exec_()

1 个答案:

答案 0 :(得分:0)

我无法在我的系统上重现这一点。

快速谷歌搜索会返回this结果,表明该消息是由系统上安装的“Viewfinity”产品引起的,并且不是PyQt的问题。看来该产品已修改了本机系统文件对话框。您可以通过卸载产品来避免它,或者在创建对话框时要求Qt不使用带有Qt.DontUseNativeDialog选项的本机文件对话框。