简体   繁体   中英

How do I use one object from one function in another function in the same class

I want to use "img" of browse_file() in grey_scale() . I am able to use band_count(type Int) but when I am trying to use "img" in grey_scale() , I'm getting the following error:

the type of "img" is class 'spectral.io.bilfile.BilFile'
Traceback (most recent call last):
File "main.py", line 50, in grey_scale
view = imshow(img,(bandGrey,))`
NameError: global name 'img' is not defined`

My code:

def browse_file(self,MainWindow):
    file = str(QtGui.QFileDialog.getOpenFileName(self,"Select Directory"))
    img = envi.open(file)       #load image to img object.
    band_info = str(img.read_band) 
    band_count = int((band_info.split(start))[1].split(end)[0])
    view = imshow(img,(1,0,0))

def grey_scale(self):
    bandGrey = self.spinBox_grey_band.value()
    print bandGrey      #working
    view = imshow(img,(bandGrey,))  #error 

It looks like you need to make img an instance attribute, so that it gets "saved" in self between calls to browse_file and grey_scale :

def browse_file(self,MainWindow):
    file = str(QtGui.QFileDialog.getOpenFileName(self,"Select Directory"))
    img = envi.open(file)       #load image to img object.
    band_info = str(img.read_band) 
    band_count = int((band_info.split(start))[1].split(end)[0])
    view = imshow(img,(1,0,0))
    self.img = img

def grey_scale(self):
    bandGrey = self.spinBox_grey_band.value()
    print bandGrey      #working
    view = imshow(self.img,(bandGrey,))  #error 

This means, of course, that you need to ensure that browse_file is called before grey_scale , so that self.img is defined before it is used.

The point of local variables is that they only exist within the function, so you can't do this by definition.

Often, the answer is to pass the variable's value around. For example, browse_file could return img to its caller, and that caller could keep it around and then pass it into grey_scale later.

Another option is to use a class to hold state. These functions seems to be methods of a class already (based on the self parameter), so there's a good chance this is the right design here. Just replace every img in both functions with self.img , and now this isn't a local variable, it's a member of the instance. Every method called on the same instance will have access to the same value. But if you create multiple instances of the same class, each instance will have its own img .

You need to return the img from the function browse_file()

Then create a new vaiable from the function.

next add the img to the list of arguments in grey_scale()

def browse_file(self,MainWindow):
    file = str(QtGui.QFileDialog.getOpenFileName(self,"Select Directory"))
    img = envi.open(file)       #load image to img object.
    band_info = str(img.read_band) 
    band_count = int((band_info.split(start))[1].split(end)[0])
    view = imshow(img,(1,0,0))
    return img

img = browse_file(self,MainWindow)

def grey_scale(self, img):
    bandGrey = self.spinBox_grey_band.value()
    print bandGrey      #working
    view = imshow(img,(bandGrey,))  #error 

grey_scale(img)

you can reuse img inside the class.

If you wish to use the img variable outwith of the class, you can either make it global

global img
img = browse_file(self,MainWindow)

or create a variable from the class.

ie.

img = class_object.browse_file(MainWindow)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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