[Python]GUI编程练习 -- 获取天气预报
2013-04-25 16:51
405 查看
第一个小应用: 桌面天气
简单的一个桌面窗口,显示某地区的天气情况,每小时一次
来源,用户:
自己
价值:
编程练习
首先解决获取json并解析,然后是窗口显示。
![](http://img.my.csdn.net/uploads/201304/25/1366880172_6003.png)
有些东西没有用过就是不知道会出现什么问题,可能一点点的问题就会把时间托的很久,所以一定要把功底弄扎实,还要有灵活多变的应对策略,解决方法的思路和快速的思考能力是要有意识的培养才行。就是因为一个json字典转换的编码问题弄了好长时间。。。真需要多练习,加油。
需求:
背景:简单的一个桌面窗口,显示某地区的天气情况,每小时一次
来源,用户:
自己
价值:
编程练习
设计:
使用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()
测试:
![](http://img.my.csdn.net/uploads/201304/25/1366880172_6003.png)
总结:
很简陋,功能是有了,需要做的还有很多,积累经验。有些东西没有用过就是不知道会出现什么问题,可能一点点的问题就会把时间托的很久,所以一定要把功底弄扎实,还要有灵活多变的应对策略,解决方法的思路和快速的思考能力是要有意识的培养才行。就是因为一个json字典转换的编码问题弄了好长时间。。。真需要多练习,加油。
相关文章推荐
- python的GUI编程--wxpython学习(四)-->练习:一个自动测试app启动时间的小工具
- python编程练习之四——获取系统内存、CPU、磁盘、平均负载信息并保存到数据库中
- 【Python】Python 的图形界面(GUI)编程
- python GUI编程(Tkinter)
- 【脚本语言系列】关于PythonGUI编程wxPython, 你需要知道的事
- python编程:从入门到实践-第十章练习
- 鱼c笔记——Python的GUI编程(十六):Tkinter的三大布局管理器pack、grid和place
- 《Python核心编程》第二版第407页第十三章练习 续六 -Python核心编程答案-自己做的-
- Dave Python 练习十九 -- 网络客户端编程
- Python -- Gui编程 -- Tkinter的使用 -- 基本控件
- Python计算机视觉编程练习6:文本、列表操作
- python GUI编程——Scrollbar&&Listbox
- python核心编程十三章练习
- Python GUI练习
- Python 编程核心 - Chapter9练习
- 《Python核心编程》第二版第230页第九章练习 -Python核心编程答案-自己做的-
- [JavaScript编程练习]js获取文字中的100,使其变为红色,若文字100改为其他任意数字,该数字依然会是红色
- 【Python】GUI 练习1--利率计算器
- Python入门小练习 003 利用cookielib模拟登录获取账户信息
- Python核心编程 第七章 练习7–5