Tkinter功能在GUI中打印 [英] Tkinter function print in GUI

查看:119
本文介绍了Tkinter功能在GUI中打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用2个api完成了一个程序,该程序显示了用户选择的不同城市的预测和城市信息.但是,现在我需要帮助,因为我陷入了如何获取我的make_request,make_requests和city_data在GUI中打印信息的问题!我想我需要一个标签,但是我不知道该怎么做,任何提示/帮助都很好!谢谢:

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

推荐答案

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

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

如果只想显示几条数据,则可以使用标签窗口小部件,并使用config方法更改它们中显示的内容.例如:

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)

这篇关于Tkinter功能在GUI中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆