Search code examples
pythonclassobjecttkinterattributes

AttributeError: '' object has no attribute ''


I have a problem, I was working on a code with python 3. the code is about getting news of a website onto my canvas. however I keep getting this error which says:

AttributeError: 'NewsFeed' object has no attribute 'canvas'.

this is my code:

from tkinter import *
import feedparser

root=Tk()


class Achtergrond() :
    m_Width = 0
    m_Height = 0
    m_BgColor = 0


    def CreateCanvas(self, master, width, height) :
        m_Width = width
        m_Height = height
        m_BgColor='#14B4E1'
        self.canvas = Canvas(master, width=m_Width, height=m_Height)
        self.canvas.configure(bg=m_BgColor)
        self.canvas.pack()
        return(self.canvas)

    def create_rectangle(x0, y0 ,x1,y1, color) :
        self.canvas.create_rectangle(x0, y0, x1, y1, fill=color)
        return

    def create_title (self, x, y, title, opmaak):
        self.canvas.create_text(x,y,text=title, font= opmaak)
        return

    def create_newsbox(master,x0,y0,x1,y1,color):
        canvas.create_rectangle(x0,y0,x1,y1, fill=color)
        return 

    def SetBackgroundColor(self, kleurcode) :
        m_BgColor = kleurcode
        self.canvas.configure(bg=m_BgCode)
        self.canvas.pack()
        return

class NewsFeed ():
    Hitem = 0
    Nitem = 0
    feed = 0
    th = 0
    rssURL = ""



    def UpdateNews(self,path):
        self.rssURL = path
        self.feed = feedparser.parse(self.rssURL)
        self.Nitem = len(self.feed["items"])
        return

    def ToonEerste(self):
        str=""
        self.Hitem = 0
        for i in range(self.Hitem,self.Hitem+2,1):
            if i < self.Nitem:
              entry = self.feed["entries"][i]
              str += entry.title + "\n\n" + entry.summary +      "\n__________________________________________________\n\n"
        self.canvas.delete(th)
        th = self.canvas.create_text(200,300, width=300, text = str)
        return

    def ToonVolgende(self):
        str=""
        self.Hitem += 3
        if self.Hitem >= self.Nitem:
            Hitem = 0
        for i in range(self.Hitem,self.Hitem+2,1):
            if i < self.Nitem:
              entry = feed["entries"][i]
              str += entry.title + "\n\n" + entry.summary + "\n__________________________________________________\n\n"
        self.canvas.delete(th)
        th = self.canvas.create_text(200,300, width=300, text = str)
        return




hoofdscherm = Achtergrond()
a = hoofdscherm.CreateCanvas(root, 1024,600)
b = hoofdscherm.canvas.create_rectangle(30, 120, 360, 500, fill= '#ffffff')
a.create_rectangle(10,10, 1014,80, fill='#ffffff')
a.create_rectangle(30, 120, 360, 500, fill= '#ffffff')
a.create_text(250, 50, text="Harens Lyceum" , font=('Calibri' , 40))
a.configure(bg='#14B4E1')
a.pack()

n = NewsFeed()
n.UpdateNews('http://www.nu.nl/rss/Algemeen')
n.ToonEerste(root)
n.ToonVolgende(root)





root.mainloop()

We would like to have some help with our problem, we don't have much experience. We need to make a screen with raspberry which displays news etc. for school. If you know how we can fix our code we would very much appreciate your guys' help.


Solution

  • Your NewsFeed class instance n doesn't have a Canvas attribute. If you want to pass the Canvas defined in your Achtergrond class instance hoofdscherm to n, you can define it under the class definition for NewsFeed using __init__():

    class NewsFeed ():
        def __init__(self, canvas):
            self.canvas = canvas
        ...
    

    Then when you initialize your NewsFeed object as n, you can pass the Canvas instance from your Achtergrond class instance hoofdscherm:

    n = NewsFeed(hoofdscherm.canvas)
    

    This is a solution to your current issue, but there are other errors in your code that you can see once you modify it.