github项目地址:StarMan

Python 实现天气查询的程序早已完成,近日开学无课,昨晚心血来潮想做一个较为友好的界面版本,便匆忙行动了起来。

在之前已有的程序的基础上使用Tkinter 模块实现GUI 并不是很难,但是在做的过程中《我的英雄学院》更新了,所以中途耽误了,今天早上才做好。(~.~)

代码的主体是爬虫与Tkinter。

执行程序后会先出现一个选择城市的界面,这里需要输入城市名。点击确认即会出现城市天气状况。

执行效果如下:

以下为源代码:

from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox root = Tk() def main():
#输入窗口
root.title('天气查询')#窗口标题
Label(root,text = '请输入城市').grid(row=0,column=0)#设置标签并调整位置
enter = Entry(root)#输入框
enter.grid(row = 0,column=1,padx = 20, pady = 20)#调整位置
enter.delete(0,END)#清空输入框
enter.insert(0,'湘潭')#设置默认文本
#enter_text = enter.get()#获取输入框的内容 running = 1 def get_weather_data() :#获取网站数据
city_name = enter.get()#获取输入框的内容
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
#网址1只需要输入城市名,网址2需要输入城市代码
#print(url1)
weather_data = urllib.request.urlopen(url1).read()
#读取网页数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
#解压网页数据
weather_dict = json.loads(weather_data)
#将json数据转换为dict数据
if weather_dict.get('desc') == 'invilad-citykey':
print(messagebox.askokcancel("xing","你输入的城市名有误,或者天气中心未收录你所在城市"))
else:
#print(messagebox.askokcancel('xing','bingguo'))
show_data(weather_dict,city_name) def show_data(weather_dict,city_name):#显示数据
forecast = weather_dict.get('data').get('forecast')#获取数据块
root1=Tk()#副窗口
root1.geometry('650x280')#修改窗口大小
root1.title(city_name + '天气状况')#副窗口标题 #设置日期列表
for i in range(5):#将每一天的数据放入列表中
LANGS = [(forecast[i].get('date'),'日期'),
(forecast[i].get('fengxiang'),'风向'),
(str(forecast[i].get('fengji')),'风级'),
(forecast[i].get('high'),'最高温'),
(forecast[i].get('low'),'最低温'),
(forecast[i].get('type'),'天气')]
group = LabelFrame(root1,text = '天气状况',padx = 0,pady = 0)#框架
group.pack(padx=11,pady=0,side = LEFT)#放置框架
for lang, value in LANGS:#将数据放入框架中
c = Label(group,text = value + ': ' + lang)
c.pack(anchor = W)
Label(root1,text = '今日' + weather_dict.get('data').get('ganmao'),
fg = 'green').place(x=40,y=20,height=40)#温馨提示
Label(root1,text = "StarMan: 49star.com",fg = "green",bg = "yellow").place(x=10,y=255,width=125,height=20)#作者网站
Button(root1,text = '确认并退出',width=10,command = root1.quit).place(x=500,y=230,width = 80,height=40)#退出按钮
root1.mainloop() #布置按键
Button(root, text = "确认",width=10,command = get_weather_data)\
.grid(row = 3, column=0,sticky = W, padx = 10, pady = 5)
Button(root, text = '退出',width=10,command = root.quit)\
.grid(row = 3, column = 1, sticky = E, padx = 10, pady = 5)
if running==1:
root.mainloop() if __name__ == '__main__':
main()

今天海贼王更新!!共勉!

最新文章

  1. 【干货分享】流程DEMO-付款申请单
  2. realmswift的使用
  3. HDU 1358 简单kmp
  4. spark1.4加载mysql数据 创建Dataframe及join操作连接方法问题
  5. J2EE(java)后台调用ArcGIS Engine(AE)的部署和代码
  6. 用户 IIS APPPOOL\DefaultAppPool 登录失败的解决方法
  7. 如何调整 php 应用的上传附件大小?
  8. [Redux] Generating Containers with connect() from React Redux (AddTodo)
  9. ZooKeeper集群安装
  10. 【转载】javadoc学习笔记和可能的注意细节
  11. 关于个人编辑器sublime text3使用指南
  12. day06 Request Response
  13. Windows下配置 Nginx + PHP
  14. 异常:tomcat与windows时间不同步
  15. elasticsearch数据结构
  16. APS.NET MVC4生成解析二维码简单Demo
  17. js获取url传值的方法
  18. PTA 7-2 符号配对(栈模拟)
  19. find -size 查出指定文件大小的命令
  20. 可视化库-seaborn-多变量分析绘图(第五天)

热门文章

  1. CentOS7.5安装GitLab及汉化
  2. c++学习笔记_1
  3. SSH连接时root用户无法验证通过
  4. rest_framework之序列化组件
  5. python解析jSON文件
  6. Redis 根据Key模糊批量查询数据
  7. Redis内存分析工具—redis-rdb-tools (转载http://www.voidcn.com/article/p-axfdqxmd-bro.html)
  8. linux 下tomcat出现 Native memory allocation (malloc) failed to allocate 1915224064 bytes for committing reserved memory问题
  9. Java网络编程面试总结
  10. c# 金钱大写转小写工具类