[Python]GUI编程练习 -- 获取天气预报
来源:互联网 发布:淘宝悬浮导航制作方法 编辑:程序博客网 时间:2024/06/01 08:45
第一个小应用: 桌面天气
需求:
背景:简单的一个桌面窗口,显示某地区的天气情况,每小时一次
来源,用户:
自己
价值:
编程练习
设计:
使用tkinter做出一个小窗口,后天通过中央气象的json接口获得天气情况,手动更新也可以首先解决获取json并解析,然后是窗口显示。
编码:
#coding:utf8#python2.73 winxp '''天气插件: 使用json接受中气象的本地信息,显示使用tkinterurl http://www.weather.com.cn/data/sk/101221201.htmlv0.1'''from Tkinter import *import jsonimport urllibimport timedef 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 weatherinfoclass 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编程练习 -- 获取天气预报
- python获取天气预报
- python 天气预报获取
- python gui 练习
- Python GUI练习
- python 获取省份的天气预报
- Python的GUI编程
- python GUI编程
- python GUI编程
- Python GUI编程(Tkinter)
- Python GUI编程(Tkinter)
- python GUI编程
- python的GUI编程
- Python GUI 编程 工具
- Python GUI编程(Tkinter)
- Python GUI编程(Tkinter)
- Python GUI编程(Tkinter)
- python GUI编程(Tkinter)
- Ubuntu12.04 下配置qtcreator交叉编译
- [Audio] Android 耳机切换
- 不使用JQuery的多行新闻滚动
- Spring AOP 配置
- 窗口句柄浅析和获取窗口句柄的方法
- [Python]GUI编程练习 -- 获取天气预报
- DOM节点的操作
- 反射原理获得bean的属性
- OpenWRT 增加内核模块及应用方法
- libpcap使用
- C#启动另一个应用程序并传参数
- .Net解析JSON数据
- ExtJs GridPanel Column 自动换行
- Serv-U 错误模块 msvcr80.dll 错误地址 0x0001500a 临时解决方式