2

I have done a program with 2 apis that show forecast and city info of diffrent citys that the user choose. But now I need help cause Im stuck on how to get my make_request, make_requests and city_data to print the info in the GUI! I think I need a Label but I dont know how to do it, any tips/help would be great! Thanks:

from tkinter import *
import requests
import json

    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")

        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()
            for day in data['forecast']['simpleforecast']['forecastday']:
                print (day['date']['weekday'] + ":")
                print ("Conditions: ", day['conditions'])
                print ("High: ", day['high']['celsius'] + "C", "Low: ", day['low']['celsius'] + "C", '\n')
            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()
            for info in datan['response']['data']:
                print ("Place:", info['title'])
                print ("Category:", info['maincategory'])
                print ("Likes:", info['totallikes'], '\n')
            return datan

        def city_data(self):
            cityList = requests.get ("http://api.v1.trippinin.com/citylist?key=58ffb98334528b72937ce3390c0de2b7")
            cityData = cityList.json()
            for cityInfo in cityData['response']['data']:
                print ("City:", cityInfo['title'])
                print ("Places we cover:", cityInfo['totalPlaces'], '\n')
            return cityData






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

    app = Application(master=rot)
    app.mainloop()
8
  • What do you mean by you "don't know how to do it"? What is it you don't understand? Did you create the code that creates the Entry widgets? Labels are only slightly different then Entry widgets; if you can create one you should be able to create the other. Commented Dec 3, 2013 at 11:38
  • I can create a Label but I dont know how to make the requested apis to get printed in the gui. Dont know what to call on cause every time I do a label and call on make_requests nothing happens Commented Dec 3, 2013 at 11:41
  • You have Label even with StringVar in your code and you don't know how to use it ? Strange. Commented Dec 3, 2013 at 11:42
  • See self.var.set("...") in your code. Commented Dec 3, 2013 at 11:43
  • I know how to make an Label the problem is I dont understand how to get the apis printing in the GUI.. Commented Dec 3, 2013 at 11:44

2 Answers 2

1

If you just want to simulate printing to the console, you should use a text widget. The text widget is the right choice for showing multi-line data. Instead of calling print, you call insert:

def create_widgets(self):
    ...
    self.out = Text(self)
    ...

def make_request(self):
    r = requests.get(...)
    data = r.json()
    self.out.insert("end", data)
    ...

If you want to show just a few pieces of data, you can use label widgets, and change what appears in them using the config method. For example:

def create_widgets(self):
    ...
    self.label1 = Label(self, ...)
    self.label2 = Label(self, ...)

def make_request(self):
    ...
    self.label1.configure(text=something)
    self.label2.configure(text=something_else)
Sign up to request clarification or add additional context in comments.

Comments

0

Example - I use existing label with title to set information from request

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()
    for day in data['forecast']['simpleforecast']['forecastday']:
        self.var.set( day['date']['weekday'] + ":" + "\n" + "Conditions: " + day['conditions'] + "\n" + "High: " + day['high']['celsius'] + "C " + "Low: " + day['low']['celsius'] + "C" )
    return data

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.