在Python QT中的新窗口中打开一个目录

时间:2014-02-27 05:42:59

标签: python pyqt4 qfiledialog

您好我想用python QT代码打开一个新窗口。目前我正在尝试QFileialog getOpenFileName和getExistingDirectory,但是它们都没有按预期工作。需要只用给定的路径打开一个新窗口。

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()

1 个答案:

答案 0 :(得分:2)

不完全确定您要执行的操作,但看起来您需要show()主窗口:

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

然后可能会添加一个用于打开对话框的按钮:

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)