Reputation: 17
When i run it in CMD tkinter comes up and runs the program but the results/forecast comes up in CMD when i have enter a city, i want to come up in tkinter box program how do i do?
Do i need a label or what?
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()
Upvotes: 0
Views: 159
Reputation: 2698
Yes you should use labels for data to be displayed on a Tkinter window:
Here is a basic example to do so:
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()
This examples will give you idea about how to use StringVar or others to update the label.
Some info here
Upvotes: 1