属性错误有两个类

时间:2013-12-05 12:59:26

标签: python user-interface tkinter

我正在尝试做两个课程,但我得到了一个AttributeError: Application has no attribute "make_request",它也说明了make_requestscity_data

这是我的GUI:

from tkinter import *
from tripinator2 import Functions

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.root = master
        self.pack()
        self.create_widgets()


    def create_widgets(self):
        self.var = StringVar()
        self.countryImage = Label(self, textvariable=self.var, text=self.var.get)
        self.countryImage.pack(side="top")

        self.var.set("The Tripinator" '\n' "Welcome to Tripinator!" '\n' "To get forecast, enter a country and city then press forecast." '\n'"To get tips, JUST enter city then choose beetween Eat / Drink / Party / Shop / Outdoor!")
        self.var.get()

        self.enterCountry = StringVar()
        self.inputCountry = Entry(self, textvariable=self.enterCountry)
        self.inputCountry.pack(side="left")

        self.enterCountry.set("Enter Country")
        self.enterCountry.get()
        self.inputCountry.focus_set()

        self.v = StringVar()
        self.e = Entry(self, textvariable=self.v)
        self.e.pack(side="left")

        self.v.set("Enter City")
        self.v.get()
        self.e.focus_set()

        self.preCategory = StringVar()
        self.enterCategory = Entry(self, textvariable=self.preCategory)
        self.enterCategory.pack(side="left")

        self.preCategory.set("Eat/Drink/Party/Shop/Outdoor")
        self.preCategory.get()
        self.enterCategory.focus_set()

        self.preDay = StringVar()
        self.enterDay = Entry(self, textvariable=self.preDay)
        self.enterDay.pack(side="left")

        self.preDay.set("Enter day")
        self.preDay.get()
        self.enterDay.focus_set()

        self.preTime = StringVar()
        self.enterTime = Entry(self, textvariable=self.preTime)
        self.enterTime.pack(side="left")

        self.preTime.set("morning/midday/evening/night/latenight")
        self.preTime.get()
        self.enterTime.focus_set()

        # Knappen utlöser funktionen make_request som skriver ut väderprognosen
        self.butn = Button(self)
        self.butn["text"] = "Forecast"
        self.butn["command"] = self.make_request
        self.butn.pack(side="left")

        self.b = Button(self)
        self.b["text"] = "City info"
        self.b["command"] = self.make_requests
        self.b.pack(side="left")

        self.getButton = Button(self)
        self.getButton["text"] = "City list"
        self.getButton["command"] = self.city_data
        self.getButton.pack(side="left")

        self.QUIT = Button(self, text="QUIT", command=self.root.destroy)
        self.QUIT.pack(side="left")

        self.out = Text(self)
        self.out.pack(side="bottom")


rot = Tk()
rot.geometry("900x650+200+50")
rot.title("The Tripinator")

app = Application(master=rot)
app.mainloop()

以下是请求函数:

import requests
import json

class Functions():    

    def make_request(self):
        r = requests.get("http://api.wunderground.com/api/61418d709872f773/forecast/q/" + self.enterCountry.get() +"/" + self.v.get() +".json")
        data = r.json()
        try:
            for day in data['forecast']['simpleforecast']['forecastday']:
                self.out.insert ("end", '\n', "end", day['date']['weekday'] + ":" ,'\n')
                self.out.insert ("end", '\n' "Conditions: ", "end", day['conditions'] ,'\n')
                self.out.insert ("end", '\n' "High: ", "end", day['high']['celsius'] + "C", "end",'\n' "Low: ", "end", day['low']['celsius'] + "C", '\n')
        except Exception as e:
            self.out.insert ("end", "\nWrong!\n" , e)
        return data

    def make_requests(self):
        c = requests.get("http://api.v1.trippinin.com/City/" + self.v.get() + "/" + self.preCategory.get() + "?day=" + self.preDay.get() +"&time=" + self.preTime.get() + "&limit=10& offset=2&KEY=58ffb98334528b72937ce3390c0de2b7")
        datan = c.json()
        try:
            for info in datan['response']['data']:
                self.out.insert ("end", '\n' "Place:", "end", info['title'], '\n')
                self.out.insert ("end", '\n' "Category:", "end", info['maincategory'], '\n')
                self.out.insert ("end", '\n' "Likes:", "end", info['totallikes'], '\n')
        except Exception as e:
            self.out.insert ("end", "\nWrong!\n" , e)
        return datan

    def city_data(self):
        cityList = requests.get ("http://api.v1.trippinin.com/citylist?key=58ffb98334528b72937ce3390c0de2b7")
        cityData = cityList.json()
        try:
            for cityInfo in cityData['response']['data']:
                self.out.insert ("end", '\n' "City:", "end", cityInfo['title'], '\n')
                self.out.insert ("end", '\n' "Places we cover:", "end", cityInfo['totalPlaces'], '\n')
        except Exception as e:
            self.out.insert ("end", "\nWrong!\n" , e)
        return cityData

1 个答案:

答案 0 :(得分:1)

您的Functions对象必须混合到您的Application类中,才能使该方法成为该类的一部分:

class Application(Frame, Functions):

仅导入Functions类是不够的。