如何从类中的另一个函数调用变量

时间:2019-07-09 14:15:12

标签: python pyqt5

我正在尝试从同一PyQT5 class中的另一个函数调用变量,但是它返回错误。我基本上是使用文件选择器获取值并将其放在panda中,并将结果存储在data2中

我已经检查了缩进。

python

class Ui_Dialog(QWidget):

    def selectFile(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getOpenFileName(self, "Open File", "", "All Files (*);;Excel Files (*.xlsx)", options=options)
        # print(fileName)
        if fileName:
            # df=pd.read_excel(fileName,Header=None)
            self.variableOne = fileName
            # return self.variableOne
            print(self.variableOne)
            # return fileName
            # type(fileName)
            # global data
            # data=np.asarray(df)

    def pandaGetValues(self):
        self.df=pd.read_excel(self.variabelOne,Header=None)
        self.data=np.asarray(self.df)
        self.data2=self.data[1:-1,:]

    def get_voltage(self, r, t):
        all=[]
        data3=self.data2
        for i in range (1,len(data3[0]),3):
            self.mm = [] 
            times=[]
            self.times2=[]
            self.header = self.data[0,i]
            firstnumber = 0
            first_index = 0
            #temp=np.empty([], dtype=int)
            self.temp=[]
            m=np.where((data3[1:,i] >= r ) & (data3[1:,i] <= t )) 
            self.final=[]
            # print('\n\n line number',i)
            print(self.data[0,i])
            #return i
            self.mm=self.m[0]
            self.mm=np.asarray(self.mm)
            print(self.mm)

  

AttributeError:“ Ui_Dialog”对象没有属性“ data2”

1 个答案:

答案 0 :(得分:0)

您尝试调用的变量“ data2”是函数“ pandaGetValues”的局部变量。如果您希望使用self来访问它,则需要将其设置为全局:

class Ui_Dialog(QWidget):

   data2 = None

   def selectFile(self):
      ...
相关问题