简体   繁体   English

如何在同一类的另一个函数中使用一个函数中的一个对象

[英]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() .我想在browse_file()中使用grey_scale() () 的"img" 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:我可以使用band_count(type Int)但是当我尝试在grey_scale()中使用"img"时,出现以下错误:

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 :看起来您需要将img设为实例属性,以便它在调用browse_filegrey_scale之间“保存”在self中:

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.当然,这意味着您需要确保在调用browse_file之前调用grey_scale ,以便在使用之前定义self.img

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.例如, browse_file可以将img返回给它的调用者,调用者可以保留它,然后稍后将它传递给grey_scale

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.这些函数似乎已经是一个类的方法(基于self参数),所以这很有可能是正确的设计。 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.只需将两个函数中的每个img替换为self.img ,现在这不是局部变量,而是实例的成员。 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 .但是如果您创建同一个类的多个实例,每个实例都会有自己的img

You need to return the img from the function browse_file()您需要从函数browse_file()返回img

Then create a new vaiable from the function.然后从该函数创建一个新变量。

next add the img to the list of arguments in grey_scale()接下来将 img 添加到 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.您可以在课堂上重复使用 img。

If you wish to use the img variable outwith of the class, you can either make it global如果你想在类之外使用 img 变量,你可以将它设为全局

global img
img = browse_file(self,MainWindow)

or create a variable from the class.或从类中创建一个变量。

ie. IE。

img = class_object.browse_file(MainWindow)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个类的一个类调用函数? - How do I call a function from one class in another? 如何在openerp的同一类的另一个函数中使用一个函数变量? - How to use one function variable in another function of same class in openerp? 如何从一个函数中获取一个值并在另一个函数中使用它? - How do I take a value from one function and use it in another? 如何在另一个 function 的输入中使用? - How do I use in input from one function in another? 如何在一个函数和另一个函数中使用相同的变量,而又不将该变量设置为全局变量? - How do I use the same variable in one function with another function, without making that variable Global? 如何在另一个函数中使用一个函数收集的数据 - How do I use the data collected of one function in another function 如何将一个 function 的 output 用于另一个 function? - How do I use the output of one function for another function? 如何将变量从一个函数发送到另一个函数? - How do I send a variable from one function to another function? 如何在另一个类的方法中使用来自单独类的一个对象? - How do I use one object from separate class in another class's method? 如何在python中的另一个函数中使用一个函数中的数据? - How do I use data from one function in another function in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM