AttributeError:“对象没有属性" [英] AttributeError: '' object has no attribute ''
问题描述
我有一个问题,我正在使用python 3编写代码.该代码用于将网站新闻发布到画布上.但是,我不断收到此错误,内容为:
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"对象没有属性"canvas".
AttributeError: 'NewsFeed' object has no attribute 'canvas'.
这是我的代码:
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.
推荐答案
您的NewsFeed
类实例n
没有Canvas
属性.如果要将在Achtergrond
类实例hoofdscherm
中定义的Canvas
传递给n
,则可以使用__init__()
在NewsFeed
的类定义下进行定义:
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
...
然后,当将NewsFeed
对象初始化为n
时,可以从Achtergrond
类实例hoofdscherm
传递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.
这篇关于AttributeError:“对象没有属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!