Maham
Maham

Reputation: 442

Getting user input in one function of the class and using it in another in Python

I am trying to create a class in Python, having two functions "Dataset" and "LoadImages". Here is the code:

class GUI:
  def __init__(self):
    self.File=[];

  def Dataset(self):
    self.File = askdirectory(initialdir="D:/",title='Load the dataset.')
    return self.File

  def LoadImages(self):
    print self.File
    paths = []
    for fname in os.listdir(self.File):
        if fname.split(".")[-1] in ALLOWED_EXTENSIONS:
            paths.append(os.path.join(self.File, fname))

I want the user to input a directory in "Dataset" and then save it in self.File, and now I want the function "LoadImages" to read the image files in the path self.File specified by the user. I am first calling GUI().Dataset, that will get an input directory from the user, and then GUI().LoadImages to get read the images in a directory. But the second time I call GUI().LoadImages,the class is again initialized and self.File is set to []. How to do that?

Upvotes: 1

Views: 123

Answers (1)

Arctelix
Arctelix

Reputation: 4576

If i understand you correctly...

When you call GUI() you are instantiating the class. If you want to make a second call to the same GUI instance you need to assign it to a variable like this:

my_gui = GUI()
my_gui.Dataset()
my_gui.LoadImages()

There are many other stylistic issues with your code, and i did not even look at the contents of the methods. Most importantly, only class names should be capitalized, method and variable names should be lowercase. With that being said, the above code should answer your question.

Upvotes: 1

Related Questions