[英]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_file
和grey_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.