前两天看到一篇关于Python使用Tkinter 的博文,写的很好。就拿来研究了一下,改了改。现分享如下:

参考

代码

# coding:utf8
# python2.73 winxp
'''''
天气插件: 使用json接受中气象的本地信息,显示使用tkinter
url http://www.weather.com.cn/data/sk/101221201.htmlv0.1
'''
from Tkinter import *
import json
import urllib
import 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.messagereturn 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()

效果

我的修改

中国城市代码

城市代码获取

代码

# coding:utf-8
import sysreload(sys)
sys.setdefaultencoding('utf8')
#    __author__ = '郭 璞'
#    __date__ = '2016/8/26'
#    __Desc__ = 天气预报小案例from Tkinter import *
import urllib2
import jsondef getWeatherInfoByCityCode(citycode):url = r'http://www.weather.com.cn/data/cityinfo/'+str(citycode)+'.html'data = urllib2.urlopen(url).read().decode('utf8')json_info = json.loads(data)weatherinfo = {}weatherinfo['city'] = json_info['weatherinfo']['city']  # 城市weatherinfo['tempH'] = json_info['weatherinfo']['temp2']  # 高温weatherinfo['tempL'] = json_info['weatherinfo']['temp1']  # 低温weatherinfo['weather'] = json_info['weatherinfo']['weather']  # 天气weatherinfo['ptime'] = json_info['weatherinfo']['ptime']  # 发布时间return weatherinfodef show_in_tkinter(ccode):tk = Tk()citycode = StringVar()citycode.set(ccode)weatherinfo = getWeatherInfoByCityCode(citycode.get())frame1 = Frame(tk)Label(frame1,text='输入城市代码').pack(side=LEFT,expand=YES)Entry(frame1,textvariable=citycode).pack()frame1.pack(side=TOP)frame2 = Frame(tk)Label(frame2, text='城市名称').pack(side=LEFT, expand=YES)cityname = '%s'%weatherinfo['city']str_v_cityname = StringVar()str_v_cityname.set(cityname)Entry(frame2, textvariable=str_v_cityname,state='readonly').pack()frame2.pack(side=TOP)frame3 = Frame(tk)Label(frame3, text='     高温').pack(side=LEFT, expand=YES)temp_h = '%s' % weatherinfo['tempH']str_v_temp_h = StringVar()str_v_temp_h.set(temp_h)Entry(frame3, textvariable=str_v_temp_h, state='readonly').pack()frame3.pack(side=TOP)frame4 = Frame(tk)Label(frame4, text='     低温').pack(side=LEFT, expand=YES)temp_l = '%s' % weatherinfo['tempL']str_v_temp_l = StringVar()str_v_temp_l.set(temp_l)Entry(frame4, textvariable=str_v_temp_l, state='readonly').pack()frame4.pack(side=TOP)frame5 = Frame(tk)Label(frame5, text='     天气').pack(side=LEFT, expand=YES)weather = '%s' % weatherinfo['weather']str_v_weather = StringVar()str_v_weather.set(weather)Entry(frame5, textvariable=str_v_weather, state='readonly').pack()frame5.pack(side=TOP)frame6 = Frame(tk)Label(frame6, text='更新时间').pack(side=LEFT, expand=YES)updatetime = '%s' % weatherinfo['ptime']str_v_updatetime = StringVar()str_v_updatetime.set(updatetime)Entry(frame6, textvariable=str_v_updatetime, state='readonly').pack()frame6.pack(side=TOP)frame7 = Frame(tk)btn_pull = Button(frame7,text='获取数据',command=lambda:show_in_tkinter(citycode.get()))print citycode.get()# btn_pull.bind(citycode.get(),update_weather)btn_pull.pack(side=LEFT, expand=YES)btn_quit = Button(frame7,text='退出',command=tk.quit).pack(side=LEFT, expand=YES)frame7.pack(side=TOP)tk.mainloop()if __name__=="__main__":show_in_tkinter(101180308)

测试结果

不足之处

本来设计的是可以输入城市代码,来实时的获取对应的天气信息,并更新到界面上的,但是不知道为什么系统会默认新开一个Tkinter的界面,导致刷新失败!

有时间再来更新本文,打个时间戳:2016年8月26日22:50:14

Python Tkinter小试相关推荐

  1. python tkinter设置窗口大小_Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸实例...

    我就废话不多说了.大家直接看代码吧! #!/usr/bin/env python #coding=utf-8 ''' 窗口屏幕居中,设置窗口最大,最小尺寸... 版权所有 2014 yao_yu (h ...

  2. python tkinter库、添加gui界面_使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)...

    使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二),创建一个,界面,布局,文件,路径 使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二) ...

  3. python界面不同按钮实现不同功能-python tkinter实现界面切换的示例代码

    跳转实现思路 主程序相当于桌子: import tkinter as tk root = tk.Tk() 而不同的Frame相当于不同的桌布: face1 = tk.Frame(root) face2 ...

  4. tkinter回调异常_处理python tkinter中的异常

    我在 Python Tkinter中编写了一个应用程序.我最近注意到,对于其中一个操作,如果该操作失败,它有时会关闭(不会给出任何错误).我写了一个小程序来说明问题: – import os from ...

  5. python Tkinter Text的简单用法

    1.设置python Tkinter Text控件文本的方法 text.insert(index,string)  index = x.y的形式,x表示行,y表示列 向第一行插入数据,text.ins ...

  6. python tkinter输入框_python TKinter获取文本框内容的方法

    python TKinter获取文本框内容的方法 更新时间:2018年10月11日 11:36:08 作者:biubiuzzz 今天小编就为大家分享一篇python TKinter获取文本框内容的方法 ...

  7. python tkinter chk

    python tkinter chk 视频过程中的练习, 可以在python2.7下运行.001: hello,world:1 2 3 4 5 6 from Tkinter import Label, ...

  8. python tkinter计算器实例_Python编程使用tkinter模块实现计算器软件完整代码示例

    Python 提供了多个图形开发界面的库.Tkinter就是其中之一. Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数 ...

  9. python点名代码_基于python tkinter的点名小程序功能的实例代码

    基于python tkinter的点名小程序功能的实例代码,花名册,次数,窗口,未找到,初始化 基于python tkinter的点名小程序功能的实例代码 易采站长站,站长之家为您整理了基于pytho ...

最新文章

  1. HDU4920(矩阵连乘)
  2. html 如何判断文本溢出,判断文本是否溢出
  3. 蓝桥杯 ADV-108算法提高 分数统计
  4. [转]介绍几个C#正则表达式工具
  5. mysql left join 耗时_性能调优:mysql之left join
  6. boost mutex 应用
  7. 计算机office demo,办公软件应用(Office2007)中级_DEMO盘-2013
  8. 那智机器人程序备份复原方法
  9. 【Python】断言(assert)的用法,你真的懂吗?
  10. LINUX rhcsa小练习题(创建文件/目录,移动/复制文件,重定向/追加重定向,创建别名)
  11. 医院在线预约挂号系统开源
  12. 爬虫技术:携程爬虫阳光问政数据
  13. 厦门大学计算机英语考试,【图片】一战厦大计算机上岸,经验帖。慢更【考研吧】_百度贴吧...
  14. 安卓bmi项目_bmi计算器
  15. 软件开发过程与项目管理(14.项目核心计划执行控制)
  16. C#游戏跨服架构进化之路
  17. FileHelpers Library
  18. 小韩实操 -- Mysql数据库的备份与恢复及安全配置
  19. 市场营销的角度探讨企业网站建设的营销策略
  20. 【学习心得】Selenium3自动化测试实战——基于Python(虫师)

热门文章

  1. Hibernate 中Datetime类型属性数据库默认值
  2. WPF 设置类库项目为启动项,设置窗体跟随。
  3. 畅通工程//最小生成树prim
  4. AndroidManifest.xml文件详解
  5. 为了成长,我所做的一些努力!
  6. myclipse 项目struts 2 版本升级过程
  7. 了解一下Flex 4里的fx、mx以及s命名空间
  8. [zz]ZeroMQ 的模式
  9. 13篇文章,教你学会ES6知识点
  10. 关于Kotlin语法的异议