在GUI中打印Tkinter功能

时间:2013-12-03 11:32:02

标签: user-interface python-3.x tkinter labels

我做了一个带有2个api的程序,显示了用户选择的不同城市的预测和城市信息。但现在我需要帮助因为我坚持如何让我的make_request,make_requests和city_data在GUI中打印信息!我想我需要一个标签,但我不知道该怎么做,任何提示/帮助都会很棒!感谢:

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

2 个答案:

答案 0 :(得分:1)

如果您只想模拟打印到控制台,则应使用文本小部件。文本小部件是显示多行数据的正确选择。您可以调用insert:

,而不是调用print
def create_widgets(self):
    ...
    self.out = Text(self)
    ...

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

如果您只想显示几个数据,可以使用标签小部件,并使用config方法更改其中显示的内容。例如:

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)

答案 1 :(得分:0)

示例 - 我使用带标题的现有标签来设置来自请求的信息

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