0

I'm trying to call a variable from another function in the same PyQT5 class but it returns error. I basically get the values using a file picker and put it in panda and store the results in data2

I have checked the indentations.

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' object has no attribute 'data2'

Harshit Agarwal
  • 878
  • 3
  • 10
  • 19
user2241397
  • 69
  • 2
  • 11

1 Answers1

0

The variable "data2" you are trying to call is a local variable for the function "pandaGetValues". If you wish to access it by using self you need to make it global:

class Ui_Dialog(QWidget):

   data2 = None

   def selectFile(self):
      ...
DanielM
  • 3,598
  • 5
  • 37
  • 53
  • 1
    This will make `data2` a class variable which means that if you have two instances of `Ui_Dialog` they will share the same `Ui_Dialog.data2` object which is very likely not what you want. Instead you should override `Ui_Dialog.__init__` and define `self.data2` there. – Heike Jul 09 '19 at 15:09