【问题标题】:PyQT - How to open a window without select folder or cancel option?PyQT - 如何在没有选择文件夹或取消选项的情况下打开窗口?
【发布时间】:2017-01-06 13:27:17
【问题描述】:

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

【问题讨论】:

  • 您可能正在寻找这个问题的答案:How to “Reveal in Finder” or “Show in Explorer” with Qt。答案是 C++,但很容易适应 python。如果您只针对没有可移植性的 Windows,您可以使用此命令 C:\Windows\explorer.exe /select,<filepath> 启动 explorer.exe。
  • @Neitsa:谢谢,它真的很有帮助。但我一直在 pyqt QfileDialog 或 pyqt 的任何其他模块中寻找方法。

标签: python pyqt4


【解决方案1】:

您可以为此使用按键事件。这是 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()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 2019-04-26
  • 2017-03-04
  • 1970-01-01
  • 2012-12-23
  • 2015-03-16
  • 1970-01-01
相关资源
最近更新 更多