如何在Pyqt中打开目录?

时间:2015-05-27 14:46:19

标签: python pyqt pyqt4

我正在配置一个按钮:

QtCore.QObject.connect(self.ui.pushButtonExport, QtCore.SIGNAL ('clicked()') ,self.'directory_to_open')

我不知道如何配置插槽以打开目录。例如C:\Example

1 个答案:

答案 0 :(得分:0)

不清楚你在问什么 - 你打开一个目录是什么意思? 您是否只想获得对目录路径的引用?

假设您的按钮在另一个类中:

button1 = QtGui.QPushButton("This is button1", self)
# set button to call somefunction() when clicked
buton1.clicked.connect(self.somefunction)

def somefunction(self):
    # this is called when button1 is clicked
    # put directory specific tasks here
    # examples:
    ddir = QtGui.QFileDialog.getExistingDirectory(self, "Get Dir PAth")
    # ddir is a QString containing the path to the directory you selected
    print ddir  # this will output something like 'C://path/you/selected'
    # lets get a list of files from the directory:
    files = [name for name in os.listdir(str(ddir))]
    # txt files only:
    files = [name for name in os.listdir(str(ddir)) if name.endswith('.txt')]
    # jpg files only:
    files = [name for name in os.listdir(str(ddir)) if name.endswith('.jpg')]
    # now do something with your directory or list of files ...
相关问题