PyQT - 如何打开没有选择文件夹或取消选项的窗口?

时间:2017-01-06 13:27:18

标签: python pyqt4

我的qt应用程序运行一个进程并将结果保存在特定目录中。

现在,用户必须转到该文件夹​​才能查看结果。通常是excel文件。

我尝试了getExistingDirectory / getOpenFileName,它打开了用于选择文件夹或打开文件的dir路径。

我只想打开一个独立于pyqt4的文件夹。 (例如:在Windows中按“Windows + E”快捷键。

from PyQt4 import QtGui,QtCore
import sys,os

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

    def openDirectory(self):
        print "Hi i am openDirectory Function . I will open Directory selected "
        openDirectoryDialog=QtGui.QFileDialog()
        print openDirectoryDialog

        # oD=openDirectoryDialog.getExistingDirectory(self,"open",os.path.abspath("..\."),openDirectoryDialog.ShowDirsOnly)  #Selectes folder
        # oD = openDirectoryDialog.directoryEntered(self,"open", os.path.abspath("..\."))
        print oD


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


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

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

您可以使用按键事件。以下是F1

的示例
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys,os

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

    def openDirectory(self):
        print "Hi i am openDirectory Function . I will open Directory selected "
        self.openDirectoryDialog=ddir = QFileDialog.getExistingDirectory(self, "Get Dir Path")
        print self.openDirectoryDialog

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_F1:
            os.system('xdg-open "%s"' % self.openDirectoryDialog)

def main():
    app = QApplication(sys.argv)
    ui=OpenDir()
    ui.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()