Search code examples
pythonpyqt5

Python using variable outside of a function (and class)


i am using PyQt5 to open a browser to get a folder and assign it to a variable folder_path, if i print it inside the function it works:

def browse_folder(self):
        global folder_path
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.ReadOnly
        folder_path = QtWidgets.QFileDialog.getExistingDirectory(
            self, "Select Folder", options=options)
        if folder_path:
            global folder_path
            self.folder_line_edit.setText(folder_path)

            self.folder_path = folder_path
            print(folder_path + 'dddwdadds')
            return folder_path

the return and the global is my try to use the variable outside the function and class so when i press submit the windows is gone and the code continues so i can use the variable, i have tried everything. the last part of the code is:

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MyApp()
    main_window.show()

    # Tamanho da window
    main_window.setGeometry(400, 400, 400, 200)
    sys.exit(app.exec_())
   

# i cannot get this to print
print(folder_path + 'ddd')

Full code:

from PyQt5 import QtWidgets, QtGui, QtCore

import sys

global folder_path


class MyApp(QtWidgets.QMainWindow):
    def __init__(self):

        super().__init__()

        self.setWindowTitle("Creator")

        # Create a central widget for the main window
        self.central_widget = QtWidgets.QWidget()
        self.setCentralWidget(self.central_widget)

        # Create a grid layout for the central widget
        self.grid_layout = QtWidgets.QGridLayout()
        self.central_widget.setLayout(self.grid_layout)

        ......

        self.submit_button.clicked.connect(self.submit_button_clicked)
        self.exit_button.clicked.connect(QtWidgets.QApplication.quit)
  

       def browse_folder(self):
        global folder_path
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.ReadOnly
        folder_path = QtWidgets.QFileDialog.getExistingDirectory(
            self, "Select Folder", options=options)
        if folder_path:
            global folder_path
            self.folder_line_edit.setText(folder_path)

            self.folder_path = folder_path
            print(folder_path + 'dddwdadds')
            return folder_path

    def browse_file(self):
        global file_path
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.ReadOnly
        file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
            self, "Select File", options=options)
        if file_path:
            self.file_line_edit.setText(file_path)
            self.file_path = file_path
            return file_path

    def submit_button_clicked(self):

        QtWidgets.QApplication.quit()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MyApp()
    main_window.show()

    # Tamanho da window
    main_window.setGeometry(400, 400, 400, 200)
    sys.exit(app.exec_())
    

print(folder_path + 'ddd')

Solution

  • If you want file_path and folder_path to be global variables you need to define them in the global scope prior to trying to assign them values from within a function or class.

    I am not entirely sure why you would need to do this but here is an example using the code you provided.

    from PyQt5 import QtWidgets, QtGui, QtCore
    import sys
    
    folder_path = None  # define variables in global scope
    file_path = None
    
    
    class MyApp(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            ...
            ...
    
        def browse_folder(self):
            global folder_path  # this will work since the variable is already defined
            options = QtWidgets.QFileDialog.Options()
            options |= QtWidgets.QFileDialog.ReadOnly
            path = QtWidgets.QFileDialog.getExistingDirectory(
                self, "Select Folder", options=options)
            if path:
                self.folder_line_edit.setText(path)
                self.folder_path = path
                print(path + 'dddwdadds')
                folder_path = path   # and this will set the new value
    
        def browse_file(self):
            global file_path    # file_path was already defined 
            options = QtWidgets.QFileDialog.Options()
            options |= QtWidgets.QFileDialog.ReadOnly
            path, _ = QtWidgets.QFileDialog.getOpenFileName(
                self, "Select File", options=options)
            if path:
                self.file_line_edit.setText(path)
                self.file_path = path
                file_path = path   # this will set a new value to file_path
    
        def submit_button_clicked(self):
            QtWidgets.QApplication.quit()
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        main_window = MyApp()
        main_window.show()
        # Tamanho da window
        main_window.setGeometry(400, 400, 400, 200)
        app.exec_()   # if you wrap this line with sys.exit() nothing that comes after will execute
        print(folder_path, file_path)  # this line will print the paths chosen once the window exits