如何在Pyqt中打开目录?

2024-05-23 17:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在配置一个按钮:

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

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


Tags: toself目录uisignalexampleconnectopen
1条回答
网友
1楼 · 发布于 2024-05-23 17:27:04

不清楚你在问什么-你说的“打开一个目录”是什么意思? 您只想获取目录路径的引用吗?在

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

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 ...

相关问题 更多 >