您的位置:首页 > 编程语言 > Python开发

[Python]GUI编程练习 -- 获取天气预报

2013-04-25 16:51 405 查看
第一个小应用:  桌面天气

需求:

背景:
简单的一个桌面窗口,显示某地区的天气情况,每小时一次
来源,用户:
自己
价值:
编程练习 

设计:

使用tkinter做出一个小窗口,后天通过中央气象的json接口获得天气情况,手动更新也可以
首先解决获取json并解析,然后是窗口显示。

编码:

#coding:utf8
#python2.73 winxp
'''
天气插件: 使用json接受中气象的本地信息,显示使用tkinter
url http://www.weather.com.cn/data/sk/101221201.html 
v0.1
'''
from Tkinter import *
import json
import urllib
import time

def getWeaInfo():
url = r'http://www.weather.com.cn/data/cityinfo/101221201.html'
res = urllib.urlopen(url)  #返回的是个json样式的字符串
weatherinfo = {}
jinfo = res.read().decode('utf8')  #jinfo是一个unicode对象,而我们要的是一个json,然后解析
info = json.loads(jinfo)  #变成键值对

if info:
try:
weatherinfo['city'] = info['weatherinfo']['city']  #城市
weatherinfo['tempH'] = info['weatherinfo']['temp1']  #高温
weatherinfo['tempL'] = info['weatherinfo']['temp2']  #低温
weatherinfo['weather'] = info['weatherinfo']['weather']  #天气
weatherinfo['ptime'] = info['weatherinfo']['ptime']  #发布时间
except KeyError,e:
print 'Do not get the key',e.message
return weatherinfo

class WeatherFrame:
'''
用于显示主框架
'''
def __init__(self):
self.root = Tk()

#一个标题
self.title = Label(self.root,text=u'天气情况')
self.title.pack(side= TOP)

#四对信息框
self.fm1 = Frame(self.root)
self.label_city = Label(self.fm1,text=u'城市')
self.label_city.pack(side=LEFT, expand=YES)
self.text_city_e = StringVar()
self.text_city = Entry(self.fm1, text='', state='readonly', textvariable=self.text_city_e)
self.text_city.pack(side=LEFT, expand=YES)
self.fm1.pack(side=TOP)

self.fm2 = Frame(self.root)
self.label_temph = Label(self.fm2,text=u'高温')
self.label_temph.pack(side=LEFT, expand=YES)
self.text_temph_e = StringVar()
self.text_temph = Entry(self.fm2, text='', state='readonly', textvariable=self.text_temph_e)
self.text_temph.pack(side=LEFT, expand=YES)
self.fm2.pack(side=TOP)

self.fm3 = Frame(self.root)
self.label_templ = Label(self.fm3,text=u'低温')
self.label_templ.pack(side=LEFT, expand=YES)
self.text_templ_e = StringVar()
self.text_templ = Entry(self.fm3, text='', state='readonly', textvariable=self.text_templ_e)
self.text_templ.pack(side=LEFT, expand=YES)
self.fm3.pack(side=TOP)

self.fm4 = Frame(self.root)
self.label_weather = Label(self.fm4,text=u'天气')
self.label_weather.pack(side=LEFT, expand=YES)
self.text_weather_e = StringVar()
self.text_weather = Entry(self.fm4, text='', state='readonly', textvariable=self.text_weather_e)
self.text_weather.pack(side=LEFT, expand=YES)
self.fm4.pack(side=TOP)

#两个操作按钮
self.fm5 = Frame(self.root)
self.button_pull = Button(self.fm5, text=u'获取', command=self.pullWeaInfo)
self.button_pull.pack(side=LEFT, expand=YES)
self.button_quit = Button(self.fm5, text=u'退出', command=self.root.quit)
self.button_quit.pack(side=LEFT, expand=YES)
self.fm5.pack(side=TOP)

self.pullWeaInfo()

def pullWeaInfo(self):
#推送天气信息,初始化推送,手动更新
weainfo = getWeaInfo()
self.text_city_e.set(weainfo['city'])
self.text_temph_e.set(weainfo['tempH'])
self.text_templ_e.set(weainfo['tempL'])
self.text_weather_e.set(weainfo['weather'])
print ('lastest updated time is  %s'%time.ctime())

if __name__=='__main__':
wf = WeatherFrame()
mainloop()


测试:



总结:

很简陋,功能是有了,需要做的还有很多,积累经验。
有些东西没有用过就是不知道会出现什么问题,可能一点点的问题就会把时间托的很久,所以一定要把功底弄扎实,还要有灵活多变的应对策略,解决方法的思路和快速的思考能力是要有意识的培养才行。就是因为一个json字典转换的编码问题弄了好长时间。。。真需要多练习,加油。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python GUI 天气 json