[Pyqt5 accessing violation of variable storing folder_path taken from getExistingDirectory function into another function]
created a simple GUi using pyqt5 where I am taking the input directory path using the get existing directory function and I am storing it in a variable but I am unable to access the variable or the folder path in another function. In the Gui I take two paths one is input folder path and the other is output folder path and I created a 3rd button which when clicked will go into a function and then i am trying to use the both paths taken and send them as input arguments to another py file and run it using subprocess but I am unable to get the folder paths in my third function and I want to pass both paths as input arguments. What will be the best way to do it. here is the code
class App(QWidget): def init (self): super(). init () self.setWindowTitle(‘Avizo’) self.setWindowIcon(QtGui.QIcon(‘logo.png’)) self.resize(600,239)
self.button1 = QPushButton('Input folder')
self.button1.setGeometry(QtCore.QRect(30, 30, 681, 28))
self.button2 = QPushButton('Select Outputfolder')
self.button2.setGeometry(QtCore.QRect(30, 80, 681, 28))
self.button4 = QPushButton('calculate')
self.button4.setGeometry(QtCore.QRect(30, 180, 161, 28))
layout = QVBoxLayout()
input_path = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Output Folder')
output_path = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select Output Folder')
if name == 'main': app = QApplication(sys.argv)
Demo = App()
Your indentation in the message is unclear.
get_input_dir is a method of class App. That means that if you use it in
cropmodule you need to prefix it with self,
get_Output_dir method does not return anything and
output_path is a local variable. It will be lost when returning. Return it, as you do for the
Thank you. I tried it but this is not what i want. i want to access the path taken as input in get_input_dir in the cropmodule method and then used it as an input argument in a subprocess.
That is about the first part of your wish list, using
get_input_dir in the
The documentation of subprocess.Popen can be found at:
It describes the arguments as follows:
args should be a sequence of program arguments or else a single string or path-like object. By default, the program to execute is the first item in args if args is a sequence.
I do not know what argument
file.py expects, but in the easiest of cases it would read:
[‘python’, ‘file.py’, self.get_input_dir(), self .get_output_dir()]
A sequence (a list in this case) with all four arguments. I have not taken into account that you should sanitize your inputs, i.e. check that using the supplied input and output directories are not damaging to your system.
file.py is the name of the file and here is the ting i dont have if name == ‘main’: statement in it so I dont know how i can pass the two paths which i have taken as input to the file.py and then run it.
You are moving the goal posts all the time.
What file? I thought it was the Python source you were wanting to run?
So this is a completely different question from the one discussed earlier. If you don’t know how that works, why use subprocess? Why not simply import file.py in this script, or import the getting of the directories in the file.py script? Subprocess is like trying to kill a mosquito with a cannon.
I think you are looking for a tutor, and this is not a tutoring forum. There is a python tutor mailing list, maybe try there.