2015-05-27 8 views
0

Я конфигурирование кнопки:Как открыть каталог в Pyqt?

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

Я не знаю, как настроить слот, чтобы открыть каталог. Например, C:\Example.

+0

Там нет такого понятия; Вы можете '' os.listdir() '' и отображать содержимое каталога. –

+1

Подключите сигнал 'clicked()' к слоту (т. Е. К функции, методу или любому другому вызываемому на Python объекту). В слоте поместите код, который показывает каталог (например, [QFileDialog.getExistingDirectory()] (http://doc.qt.io/qt-4.8/qfiledialog.html#getExistingDirectory)). – ekhumoro

ответ

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 ... 
Смежные вопросы