0

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.

6
  • 3
    Please include the full error traceback Commented Mar 13, 2018 at 14:35
  • 3
    I mean, the error you have provided tells you what's wrong. You can't use self.canvas... in the class NewsFeed as you haven't defined it within that class Commented Mar 13, 2018 at 14:39
  • how do you define the canvas in the 'newsfeed' class, without creating a new canvas. Commented Mar 13, 2018 at 14:42
  • Pass in the existing canvas as arguments to the functions within NewsFeed? Commented Mar 13, 2018 at 14:44
  • 1
    Do not use + to combine strings. Instead use the format() method to combines strings and variable data. Commented Mar 13, 2018 at 16:17

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked, you were right about the other issues 😂. Currently working on them now.
0

I had the same error. In my case, the declaration inside the constructor was in the wrong order.

A method tried to read from the attribute, before it was set. Most of the simple __init__-code does not care about order. But you just cannot read from a variable that has not been set yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.