当我在CMD中运行时,tkinter出现并运行程序但是当我进入一个城市时,结果/预测出现在CMD中,我想在tkinter box程序中出现我该怎么办?
我需要一个标签或什么?
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.v = StringVar()
self.e = Entry(self, textvariable=self.v)
self.e.pack(side="left")
self.v.set("Enter City")
self.e.focus_set()
self.butn = Button(self)
self.butn["text"] = "Forecast"
self.butn["command"] = self.make_request
self.butn.pack(side="left")
self.QUIT = Button(self, text="QUIT", command=self.root.destroy)
self.QUIT.pack(side="right")
def make_request(self):
r = requests.get("http://api.wunderground.com/api/ab78bcbaca641959/forecast/q/Sweden/" + 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
rot = Tk()
rot.geometry("900x650+200+50")
rot.title("The Trip")
app = Application(master=rot)
app.mainloop()
答案 0 :(得分:1)
是的,您应该使用标签在Tkinter窗口上显示数据:
这是一个基本的例子:
from Tkinter import *
root = Tk()
e = Entry(root)
e.pack()
var = StringVar()
def callback():
var.set(e.get())
e.focus_set()
b = Button(root, text="submit", width=10, command=callback)
b.pack()
label = Label( root, textvariable=var, relief=RAISED)
label.pack()
root.mainloop()
此示例将为您提供有关如何使用StringVar或其他更新标签的信息
一些信息here