【问题标题】:PyQt4 - How to Return and Saving the path of choosen directoryPyQt4 - 如何返回和保存所选目录的路径
【发布时间】:2019-08-20 07:54:18
【问题描述】:

我正在尝试创建一个弹出向导,让用户选择一个目录路径,将该路径保存到一个变量中,然后返回该路径以在另一个脚本中使用。

我对 Python 编码比较陌生,我只是在玩。我尝试了 Tkinter,但不喜欢图书馆的外观/感觉。虽然 PyQt 总体上更好用。我尝试以几种不同的方式设置按钮,但收效甚微。我尝试使用 YouTube 视频中显示的“self”和“self.variable”,但我不熟悉它的帮助和使用方式。

When there is a path selected, it will print the output, but it does not seem to be returning the path to use, and if it is, I am unaware of how it is doing so and how to use it in other脚本。

附带说明:我在 QtGui.QBoxLayout() 中放了一个“2”,因为没有它,代码将无法工作。

import sys
import os
from PyQt4 import QtGui



class Window(QtGui.QWizard):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(550, 350, 700, 600)
        self.setWindowTitle("Output Directory")

        path = self.home_menu()
        print("This path: {}".format(path))

        return path

    def home_menu(self):   
        out_dir = QtGui.QPushButton("Output Directory", self)
        btn1 = QtGui.QPushButton("Confirm", self)
        path = self.file_open

        out_path = out_dir.clicked.connect(path)
        print('Out_path: {}\nVariable Type: {}'.format(out_path, type(out_path)))

        btn1.clicked.connect(self.using_path) #does nothing

        btn1.resize(112, 35)
        btn1.move(556, 535)

        out_dir.resize(625,175)
        out_dir.move(30,130)


        layout = QtGui.QBoxLayout(2) #Don't know why I put '2' but code does not work without it.
        layout.addWidget(btn1)
        layout.addWidget(out_dir)
        self.show()

        return out_path

    #Sets the Style of the Window   
    def style_choice(self, text):
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(text))


    def file_open(self):
        name = QtGui.QFileDialog.getExistingDirectory()
        #self, "Choose Directory", "")
        print('path opened: {}'.format(name)) #Prints names
        return name



    def close_app(self):
        choice = QtGui.QMessageBox.question(self, 'Are you sure?',
                                            'Do you really want to exit the program?',
                                            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

        if choice == QtGui.QMessageBox.Yes:
            print('Exiting Program Now!')
            sys.exit()
        else:
            pass


    def using_path():
        try:
            directory = 0
            print("Returned path is: {}".format(directory))
            return 
        except:
            print('... progress bar failed')




def mainWindow():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    GUI.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    mainWindow()

我无法获得输出路径,并且确认按钮不起作用或执行任何操作。

感谢您提供的任何帮助。谢谢!

【问题讨论】:

  • __init__ 无法返回值 - 它在您创建实例时执行,但代码返回类的实例而不是来自 __init__ 的值
  • 为类中的变量分配路径 - 即 self.path 并在 app.exec() 之后将其获取为 GUI.path。但删除sys.exit()

标签: python directory path pyqt4


【解决方案1】:

将路径分配给self.path,然后在app.exec_() 之后获得GUI.path,但您必须跳过sys.exit()

import sys
import os
import PyQt4
from PyQt4 import QtGui

class Window(QtGui.QWizard):

    def __init__(self):
        super().__init__()

        self.path = '' # default value at start (useful if you don't select new directory)

        #layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) 
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        btn_out_dir = QtGui.QPushButton("Output Directory", self)
        btn_out_dir.clicked.connect(self.path_select)
        layout.addWidget(btn_out_dir)

        btn_confirm = QtGui.QPushButton("Confirm", self)
        btn_confirm.clicked.connect(self.close)
        layout.addWidget(btn_confirm)

    def path_select(self):
        self.path = QtGui.QFileDialog.getExistingDirectory()
        if self.path:
            print('[inside] path:', self.path)
        else:
            print('[inside] path: - not selected -')


app = QtGui.QApplication(sys.argv)

GUI = Window()
GUI.show()

status_code = app.exec_()

print('[outside] path:', GUI.path)

#sys.exit(status_code)

【讨论】:

  • 感谢您的评论和回答。它可以工作,但它不允许我将此变量发送到另一个脚本。我该怎么做?
  • 这个问题与GUI/Qt无关。通常我们 import 来自其他 Python 文件的代码并在当前程序中使用它 - 不退出 - 所以所有代码都可以访问当前变量。如果您想以不同的方式运行它,那么您可能必须将其保存在文件中,而其他脚本必须从文件中读取它。或者您使用参数"python script.py some_path" 运行脚本,然后脚本在sys.argv[1] 中获取它
猜你喜欢
  • 1970-01-01
  • 2011-10-14
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2021-12-23
  • 2016-03-17
相关资源
最近更新 更多