0

I'm trying to make a gui with pyqt5. The code works fine when I don't have the varaibales under __slots__ but once I put them in I get an error saying

"(built-in function connectSlotsByName) returned a result with an error set"

and on the top of the error it says

"'MainWindow' object has no attribute 'z'"

Below is part of my code(which is sort of long). I'm not even sure if the __slots__ is a problem. Because it worked fine when I had from 'fName' to 'has_data', but as soon as I added things under 'x', it started to give me that error.

class MainWindow(QDialog):
__slots__ = (
    # find_file_Callback
    'fName',
    'cubefilename',
    'ROI',
    'ROIcount',
    'has_data',
    'x',
    'x_end',
    'y',
    'y_end',
    'z',
    'ptflag',
    'rgflag',
    'rgflagROI',
    'intens'
    )

def __init__(self):
    super(MainWindow,self).__init__()
    loadUi("ptype1.ui",self)
    self.find_file.clicked.connect(self.find_file_Callback)
    self.micrometer.setChecked(True)
    self.start_cube.clicked.connect(self.start_cube_Callback)
    
    self.fName = ""
    self.cubefilename = ""
    self.ROI = 0
    self.ROIcount = 0
    self.has_data = 0
    self.x = 0
    self.x_end = 0
    self.y = 0
    self.y_end = 0
    self.z = 0
    self.ptflag = False
    self.rgflag = False
    self.rgflagROI = False
    self.intens = 0

    
def start_cube_Callback(self):
    self.cubefilename = self.fName[0]
    filename = self.cubefilename
    print("Working to read datacube")
    fileID = open (filename)
    data = np.fromfile(fileID, dtype = np.float32)
    x=int(data[0])
    y=int(data[1])
    z=int(data[2])
    end = len(data)
    imgZ = data[end-z:end]
    img =  data[3:x*y*z +3]
    del data
    img = img.reshape(z,x,y, order = 'F') 
    img = img.transpose(2, 1, 0) 
    img = np.flip(img,0)
    imgX = np.arange(37.5, 75*x+37.5, 75)
    imgY = np.arange(75, 150*y+75, 150)
    fileID.close()
    
    print("Working to display data\n")
    
    scalefact = 1e3

    self.x = abs((imgX-imgX[0])/scalefact)
    xm, xn = 1, len(imgX) 
    if xm > xn:
        self.x = self.x.conj().T 
    self.x_end = self.x[len(self.x) - 1]
    self.y = abs((imgY-imgY[0])/scalefact)
    ym, yn = 1, len(imgY)
    if ym < yn:
        self.y = self.y.conj().T
    self.y_end = self.y[len(self.y) - 1]
    self.z = imgZ
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Libocado
  • 43
  • 3
  • 1
    What's your reason for using `__slots__`? That feature is intended for performance in *small* and *simple* classes, QObject based classes (including anything that inherits from QWidget) certainly are not small nor simple. I suggest you to drop them, as their benefits would be negiglible. – musicamante Feb 23 '21 at 00:17
  • Oh, I'm just a beginner and was told that slots could make it run more efficiently. So, it seems like my code is too long to get any benefit from it. Thank you! Just another question on that, then how small and simple should it be to benefit from using slots? – Libocado Feb 23 '21 at 00:25
  • 1
    @Libocado It will only be beneficial when you have a very large number of instances and need to save memory. In a typical gui app, the use of `__slots__` will never make any noticeable difference. I would say it's bad advice to encourage beginners to use such features, since it's so easy to run into problems unless you know what you're doing. When you're just starting out, you should mostly forget about performance/efficiency and just concentrate on making stuff that works properly. After all, it doesn't matter how fast it is, if it doesn't actually do what it's supposed to... – ekhumoro Feb 23 '21 at 00:46

0 Answers0