简体   繁体   English

如何打开另一个pyqt gui并从中获取价值?

[英]How to open another pyqt gui and retrieve value from it?

I am working on developing a file selection dialog using Python 3.6 and pyqt5. 我正在使用Python 3.6和pyqt5开发文件选择对话框。 The basic idea of the dialog is that it has the option to preview files before selecting. 对话框的基本思想是可以选择之前预览文件。 It can preview any kinds of registered windows files. 它可以预览任何类型的已注册Windows文件。 The design was done using QtDesigner MainWindow. 使用QtDesigner MainWindow完成设计。 Now I can open this preview file browser from another pyqt/python3 file. 现在,我可以从另一个pyqt / python3文件打开此预览文件浏览器。 But how can I retrieve the selected filename and file path from that script? 但是,如何从该脚本中检索选定的文件名和文件路径? Here is the test file where I am opening the preview browser file: 这是我打开预览浏览器文件的测试文件:

class TestBrowser(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self,browser):
        super(self.__class__, self).__init__()
        self.setupUi(self)  # This is defined in design.py file automatically
        # It sets up layout and widgets that are defined
        self.browser=browser
        self.pushButton.clicked.connect(self.dario)

    def dario(self):
        self.browser.exec_()

def main():
    app = QApplication(sys.argv)
    browser=bd.BrowserDialog()
    main=TestBrowser(browser)
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':    # if we're running file directly and not importing it
    main()

I personally use globals on a different .py file (for sanity purposes) for stuff like this when I get stuck on "how can I move a value from another class/function/something". 当我陷入“如何从另一个类/函数/某物中移动一个值”的问题时,我个人将全局变量用于不同的.py文件(出于理智目的)。

On new .py file: 在新的.py文件上:

file_name = None
file_path = None

# Save function
def save_filename(selected_file, selected_path):
    global file_name, file_path
    file_name = selected_file
    file_path = selected_path

And on your browser when you select a file, add: 然后在浏览器中选择文件时,添加:

mynewpyfile.save_filename(file, path)

Then from your main window you can get the file data as: 然后,您可以从主窗口中以以下方式获取文件数据:

self.pushTestbutton.clicked.connect(self.do_something)

def do_something(self):
    print("the selected file is" + mynewpyfile.file_path + "/" + mynewpyfile.file_name)

Other alternatives: http://doc.qt.io/qt-5/qfiledialog.html 其他替代方法: http : //doc.qt.io/qt-5/qfiledialog.html
In this approach you just add a QlineEdit and... athough limited, it can't be easier than this. 在这种方法中,您只需添加QlineEdit即可,尽管有一定局限性,但没有比这更简单的了。

self.lineTest.setText(QtWidgets.QFileDialog.getOpenFileName()[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM