TypeError:'天气'对象不可调用

时间:2015-05-23 06:44:39

标签: python tkinter base64

我对编程非常陌生,所以如果我没有正确的术语,请原谅。

我试图从网站上检索图像并使用tkinter将其发布到GUI上。只要它在GUI上发布图像,它就可以工作;但是根据这篇文章的标题,它仍然会产生错误。

导致错误的代码部分如下:

    raw_data = BeautifulSoup(urllib.request.urlopen("http://www.weather.com.au/act/canberra").read())
    image = raw_data("img", ("class", "si"))[0]
    image = image["src"]
    if image == ("/images/icons/5.gif"):
        URL = urllib.request.urlopen("http://www.weather.com.au/images/icons/5.gif").read()
        b64_data = base64.encodestring(URL)
        image = PhotoImage(data = b64_data)
        label = Label(self, image = image).grid(row = 1, column = 2)
        self(image)

进一步缩小,似乎第39行导致错误。

self(image)

完整程序如下:

#!/usr/bin/env python
from tkinter import *
import sys
import urllib.request
from urllib.request import urlopen
import base64
from bs4 import BeautifulSoup

class Weather(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.configure(bg = "#ededed")
        self.grid()
        self.heading = Label(self, bg = "#ededed", text = "       Temperature      ").grid(row = 0, column = 1, pady = 5)
        self.heading = Label(self, bg = "#ededed", text = "        Picture        ").grid(row = 0, column = 2, pady = 5)

        button = Button(self, bg = "#ededed", text = "Canberra", command = self.canberra_data).grid(row = 3, column = 0, sticky = EW)

        self.obsVar1 = StringVar()
        label = Label(self, textvariable = self.obsVar1, bg = "#e5e5e5", fg = "#751974").grid(row = 1, column = 1)
        self.obsVar1.set("")

    def canberra_data(self):
        soup = BeautifulSoup(urllib.request.urlopen("http://www.bom.gov.au/act/observations/canberra.shtml").read())
        table = soup.find("table", {"id" : "tCANBERRA"}).tbody("tr", "rowleftcolumn")
        for row in soup("th", {"id" : "obs-station-canberra"}):
            for temp in soup("td", {"headers" : "obs-temp obs-station-canberra"}):                                                                       
               self.obsVar1.set(temp.text)

        raw_data = BeautifulSoup(urllib.request.urlopen("http://www.weather.com.au/act/canberra").read())
        image = raw_data("img", ("class", "si"))[0]
        image = image["src"]
        if image == ("/images/icons/5.gif"):
            URL = urllib.request.urlopen("http://www.weather.com.au/images/icons/5.gif").read()
            b64_data = base64.encodestring(URL)
            image = PhotoImage(data = b64_data)
            label = Label(self, image = image).grid(row = 1, column = 2)
            self(image)

master = Tk()
master.title("Weather Program")
app = Weather(master)
master.mainloop()

我应该补充一点,如果网站更新了它的图像,它就不再是' 5.gif' (作为一个极有可能的天气图标)然后一张图片不会出现。

非常感谢任何帮助或指示。我看上去很高,但却无济于事。提前谢谢。

2 个答案:

答案 0 :(得分:0)

包含self(图片)的行不正确。自我'不是一个阶级,它是一个对象。因此,如果您想创建一个Weather类型的对象并将其传递给它的构造函数,只需调用Weather(image)。

答案 1 :(得分:0)

您不能像函数一样调用对象。当您在代码中说self(image)时,您正试图调用该对象。如果你想这样做,你需要实现__call__方法。但是,我编写了您的示例并对其进行了更改。请运行它。我希望它有所帮助

#!/usr/bin/env python
from tkinter import *
import sys
import urllib.request
from urllib.request import urlopen
import base64
from bs4 import BeautifulSoup

class Weather(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.configure(bg = "#ededed")
        self.grid()
        self.heading = Label(self, bg = "#ededed", text = "       Temperature      ").grid(row = 0, column = 1, pady = 5)
        self.heading = Label(self, bg = "#ededed", text = "        Picture        ").grid(row = 0, column = 2, pady = 5)

        button = Button(self, bg = "#ededed", text = "Canberra", command = self.canberra_data).grid(row = 3, column = 0, sticky = EW)

        self.obsVar1 = StringVar()
        label = Label(self, textvariable = self.obsVar1, bg = "#e5e5e5", fg = "#751974").grid(row = 1, column = 1)
        self.obsVar1.set("")

    def canberra_data(self):
        soup = BeautifulSoup(urllib.request.urlopen("http://www.bom.gov.au/act/observations/canberra.shtml").read())
        table = soup.find("table", {"id" : "tCANBERRA"}).tbody("tr", "rowleftcolumn")
        for row in soup("th", {"id" : "obs-station-canberra"}):
            for temp in soup("td", {"headers" : "obs-temp obs-station-canberra"}):
               self.obsVar1.set(temp.text)

        raw_data = BeautifulSoup(urllib.request.urlopen("http://www.weather.com.au/act/canberra").read())
        image = raw_data("img", ("class", "si"))[0]
        image = image["src"]
        if image == ("/images/icons/5.gif"):
            URL = urllib.request.urlopen("http://www.weather.com.au/images/icons/5.gif").read()
            b64_data = base64.encodestring(URL)
            image = PhotoImage(data = b64_data)
            label = Label(self, image = image)
            label.image = image
            label.grid(row=1,column=2)

master = Tk()
master.title("Weather Program")
app = Weather(master)
master.mainloop()

您和我的版本的差异如下:

<             label = Label(self, image = image).grid(row = 1, column = 2)
<             self(image)
---
>             label = Label(self, image = image)
>             label.image = image
>             label.grid(row=1,column=2)