0

I am putting together a GUI for my program using PyQT5 and one of the actions is to get a folder directory and use it to access files. Hence, the user is required to select the folder location, and the program will access the folder to reach the files inside.

I have set it so the user selected folder is the variable called parent_path. Then parent_path2 is the next directory that is one folder deeper.

Here is my code:

class MainWindow(QMainWindow):
    def __init__(self):
        global ui
        QMainWindow.__init__(self)
        ui = uic.loadUi(_UI, self)  # Load the ui into self

    def browsebutton_clicked(self):
        dir_ = QFileDialog.getExistingDirectory(None, 'Select a folder:', 'C:\\', QFileDialog.ShowDirsOnly)
        self.lineEdit.setText(dir_)

        parent_path = self.lineEdit.text()
        print(parent_path)
        print(type(parent_path))

def sitebutton_clicked():
    input_site = ui.lineEdit_2.text()
    print(input_site)
    print(type(input_site))
    # WHY DOES IT FAIL TO ADD A STR VARIABLE TO ANOTHER STR VARIABLE?
    parent_path2 = parent_path + '/CongestionBackup_' + input_site
    print(parent_path2)

And here is the printed outputs for debugging:

C:/Users/****/Documents/JAG Projects/SCATS Congestion Monitor BACKUP/CongestionBackup
<class 'str'>
2326
<class 'str'>

Process finished with exit code -1073740791 (0xC0000409)

As you can see, if fails to get past the last 2 lines where parent_path2 is simply a concatenation for three strings which should form: C:/Users/****/Documents/JAG Projects/SCATS Congestion Monitor BACKUP/CongestionBackup/CongestionBackup_2326

I am also unsure of what that exit code means when it fails. I have tried this code in Jupyter Notebook, and the strings can be added together without any issues. I have also tried os.path.join() function, which also fails.

meronpan
  • 93
  • 9
  • Well, let's start with the concept of [`scope`](https://stackoverflow.com/questions/44911319/python-and-variable-scope). The reason for which it gives you an error is because `parent_path` does not exist in the *scope* of `sitebutton_clicked`. You could use `global` there also, yes, but... No, you shouldn't, since using [`global` is **bad**](https://stackoverflow.com/a/19158418). – musicamante Apr 03 '20 at 01:06
  • That seems to have fixed my issue! Now I need to learn how to use global. Why would you not recommend using ```global``` ? It does seem a bit confusing. The alternative would be for me to use ```ui.widget.text()``` to get the str from all the widgets for every single function/button click. – meronpan Apr 03 '20 at 01:31
  • If you look for it, you'll find plenty of explanations about why using global is bad, unless you **really** know why/how to use it. I can understand your confusion (we've all fallen into it), but, practically speaking, you should understand that if you're using it it's very likely that you're doing it for the wrong reasons (most likely, bad implementation or project structure). In your case, you should avoid using a global function (`sitebutton_clicked`) for signal connection, and create an instance method instead, which could use instance *attributes* to keep references for any reusable data. – musicamante Apr 03 '20 at 01:57
  • Thank you very much for the advice. As you can tell, I'm quite new to this. I'll look up instance methods. :) I've just been following how the tutorials have usually taught how to connect signals. – meronpan Apr 03 '20 at 04:32
  • Hi Dennis, I'm still unsure how to better find my definition for methods within classes to connect it to widgets. Could I contact you for some advice? – meronpan Apr 05 '20 at 23:45

0 Answers0