【问题标题】:How do I use one object from one function in another function in the same class如何在同一类的另一个函数中使用一个函数中的一个对象
【发布时间】:2018-08-17 04:13:51
【问题描述】:

我想在grey_scale() 中使用browse_file() 中的"img"。 我可以使用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`

我的代码:

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 

【问题讨论】:

    标签: python loops class object


    【解决方案1】:

    您似乎需要将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 
    

    当然,这意味着您需要确保在grey_scale 之前调用browse_file,以便在使用之前定义self.img

    【讨论】:

    • 实际上我正在使用一个函数 show_image()。在按钮的单击事件上调用 gray_scale() 。 def show_image(self,MainWindow): #works with button show。 #view = imshow(img,(1,0,0)) if(self.radioButton_greyscale.isChecked()): self.grey_scale()。我该怎么做?
    【解决方案2】:

    局部变量的要点是它们只存在于函数内部,所以不能按定义来做。

    通常,答案是传递变量的值。例如,browse_file 可以将img 返回给它的调用者,调用者可以保留它,然后稍后将它传递给grey_scale

    另一种选择是使用类来保持状态。这些函数似乎已经是一个类的方法(基于self 参数),所以很有可能这是正确的设计。只需将两个函数中的每个img 替换为self.img,现在这不是局部变量,而是实例的成员。在同一个实例上调用的每个方法都可以访问相同的值。但是如果你为同一个类创建多个实例,每个实例都会有自己的img

    【讨论】:

    • 感谢@abarnet。 self.img 为我工作。有没有更高效或模块化的方式来做到这一点?
    • @vijaysingh 我不明白你的问题。我想不出任何与您所说的“模块化”相关的东西。至于“高效”,我能想到几件事,但我怀疑它们中的任何一个都是你在想的,即使我错了,我也无法回答,不知道是哪一个。
    • @vijaysingh 但这完全是惯用的。这是存储属于某个类的每个实例的状态的正常方式,实际上这也是我们首先拥有类的主要原因。只有当img 不应该是属于类的每个实例的东西时,这才是错误的。 (例如,如果您将拥有 100 个这样的对象,但其中只有一个曾经调用 browse_file,并且它们都应该共享生成的 img,那么它应该是类属性或模块全局而是。)
    • 对不起,别介意。可能是我想太多了。谢谢你的回复。但是,有没有办法在不使其成为类变量的情况下使用它?
    • 如果其他 img 实例可以在不同的函数中具有不同的值
    【解决方案3】:

    你需要从函数browse_file()返回img

    然后从函数中创建一个新变量。

    接下来将 img 添加到 gray_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)
    

    你可以在类中重用 img。

    如果您希望在类之外使用 img 变量,您可以将其设为全局变量

    global img
    img = browse_file(self,MainWindow)
    

    或者从类中创建一个变量。

    即。

    img = class_object.browse_file(MainWindow)
    

    【讨论】:

    • 我正在使用 browse_file 来浏览文件。然后我想在不同的功能中使用“img”。那么,每次我想使用“img”时都需要打电话吗?
    • 一旦你创建了img,你就可以在不同的函数中使用这个变量没问题..
    • Thanx ,它会帮助我在其他模块中使用它。
    • 如果你有一个正确的答案。请选择绿色勾号,以便将来的用户可以找到答案。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多