简体   繁体   中英

How can I open a directory in Pyqt?

I am configuring a button:

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

I don't know how to configure the slot to open a directory. For example C:\\Example .

Its not clear what you are asking - What do you mean by 'open a directory'? Do you just want to get a reference to the path to the directory?

Assuming your button is inside another class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM