pythonpyqt4qfiledialog

Open a directory in a new window in Python QT


Hi i want to open a new window by a python QT code . Currently I am trying QFileialog getOpenFileName and getExistingDirectory however none of them is working as desired . Need to just open a new window with the given path .

from PyQt4 import QtGui,QtCore
import sys

class OpenDir(QtGui.QMainWindow):
    def __init__(self):
         super(OpenDir, self).__init__()
         self.openDirectory()

    def openDirectory(self):
        print "Hi i am openDirectory Function . I will open Directory selected "
        openDirectoryDialog=QtGui.QFileDialog()
        #oD=openDirectoryDialog.getOpenFileName(self,"open","C:/")   # open file name
        oD=openDirectoryDialog.getExistingDirectory(self,"open","C:/")  #Selectes folder


        if len(oD) > 0:
            print "accepted"
        else:
            print "nothing selected"


def main():
    app = QtGui.QApplication(sys.argv)
    ui=OpenDir()
    sys.exit(app.exec_())
#Function Main END

if __name__ == '__main__':
    main()

Solution

  • Not exactly sure what you're trying to do, but it looks like you need to show() your main window:

        ui=OpenDir()
        ui.show()
        sys.exit(app.exec_())
    

    and then maybe add a button for opening the dialog:

    class OpenDir(QtGui.QMainWindow):
        def __init__(self):
             super(OpenDir, self).__init__()
             self.button = QtGui.QPushButton('Open', self)
             self.button.clicked.connect(self.openDirectory)
             self.setCentralWidget(self.button)