Hazem
Hazem

Reputation: 13

AttributeError: 'Page' object has no attribute 'myattributename' PYQT5

I am getting this error for using self.b2 inside the function update_plot with the condition , I don't know why this error occurs despite I am using another button with the same code but it works normally.

import sys
from PyQt5.QtCore    import * 
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 
import random 
from functools import partial
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal as scipysignal
import seaborn as sb
from matplotlib.cm import get_cmap
class Page(QWidget): 
  def __init__(self, parent=None):           
        super(Page, self).__init__(parent)  
        layout   = QVBoxLayout()
        # a figure instance to plot on 
        self.figure = plt.figure() 
        self.canvas = FigureCanvas(self.figure) 
        # adding canvas to the layout 
        layout.addWidget(self.canvas)      
        # layout.addWidget(up_label)
        self.b1 = QCheckBox("Up sample")
        self.b1.setChecked(False)
        self.b1.stateChanged.connect(lambda:self.btnstate(self.b1))
        layout.addWidget(self.b1)
        self.update_plot(1)
        self.b2 = QCheckBox("Down sample")
        self.b2.setChecked(False)
        self.b2.stateChanged.connect(lambda:self.btnstate(self.b2))
        layout.addWidget(self.b2)      
        mainLayout = QGridLayout()
        mainLayout.addLayout(layout, 0, 1)
   
        self.down_counter = QSpinBox()
        layout.addWidget(self.down_counter)
        self.down_counter.setMinimum(1)
        self.down_counter.setMaximum(10)
        self.down_counter.valueChanged.connect(self.valuechange)
        self.setLayout(mainLayout)
        self.setWindowTitle("Resampling")
  def update_plot(self,value):
      
        if(self.b1.isChecked()== True):
                        upSampled.plot(upSampling(self.up_counter.value())[1],upSampling(self.up_counter.value())[0]) 
        else:
                        upSampled.plot(upSampling(1)[1],upSampling(1)[0])
        if(self.b2.isChecked() == True):
                        downSampled.plot(downSampling(self.down_counter.value())[0],downSampling(self.down_counter.value())[1]) 
        else:
                        downSampled.plot(downSampling(10)[1],downSampling(10)[0]) 
     
        # Trigger the canvas to update and redraw.
        self.canvas.draw()
  def valuechange(self):
                        if(self.b1.isChecked() == True):
                                self.l1.setText("current value:"+str(self.up_counter.value()))
                                self.update_plot(self.up_counter.value())
                        if(self.b2.isChecked() == True):
                                self.l2.setText("current value:"+str(self.down_counter.value()))
                                self.update_plot(self.down_counter.value())
  def btnstate(self,b):
                        if b.text() == "Up sample":
                                if b.isChecked() == True:
                                        self.b2.setChecked(False)
                                else:
                                        print(b.text()+" is deselected")
                                        
                        if b.text() == "Down sample":
                                if b.isChecked() == True:
                                        self.b1.setChecked(False)
                                else:
                                        print(b.text()+" is deselected")

Button b1 works with the same code but b2 gives me no attribute error any idea how to fix this?

Error Traceback:

  File "/home/hazem/Desktop/Master/DSP - LAB/assignment 3/resample.py", line 201, in <module>
    window = Page()
  File "/home/hazem/Desktop/Master/DSP - LAB/assignment 3/resample.py", line 111, in __init__
    self.update_plot(1)
  File "/home/hazem/Desktop/Master/DSP - LAB/assignment 3/resample.py", line 163, in update_plot
    if(self.b2.isChecked() == True):
AttributeError: 'Page' object has no attribute 'b2'

Upvotes: 0

Views: 641

Answers (1)

eyllanesc
eyllanesc

Reputation: 244212

Explanation

The error is trivial and to understand it, just analyze this piece of code:

# ...
# The function uses self.b2 
# but at this moment that attribute is not created yet
self.update_plot(1) 
# In the next line the attribute self.b2 is created
self.b2 = QCheckBox("Down sample")
# ...

As can be seen, the update_plot function uses the b2 attribute before it was created, so the error indicates that the attribute does not exist.

Solution:

The solution is to sort the code and move self.update_plot(1) after all the attributes used by that function are created, for example at the end of __init__:

# ...
self.setWindowTitle("Resampling")
self.update_plot(1)

Upvotes: 1

Related Questions