0

嗨,我想通过 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()
4

1 回答 1

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)
于 2014-02-27T06:00:45.023 回答