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()

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

Python 爬虫实现天气查询(可视化界面版)的更多相关文章

  1. 用 Python 编写一个天气查询应用

    效果预览: 一.获取天气信息 使用python获取天气有两种方式. 1)是通过爬虫的方式获取天气预报网站的HTML页面,然后使用xpath或者bs4解析HTML界面的内容. 2)另一种方式是根据天气预 ...

  2. 用 Python 编写一个天气查询应用 pyqt5

    ​ 效果预览: !   ​ 一.获取天气信息 使用python获取天气有两种方式. 1)是通过爬虫的方式获取天气预报网站的HTML页面,然后使用xpath或者bs4解析HTML界面的内容. 2)另一种 ...

  3. Python学习笔记——天气查询代码

    天气查询代码1 # 此程序无法运行,因为中国天气网的api接口被关闭了 import urllib.request import json import pickle #建立城市字典 pickle_f ...

  4. 基于Python爬虫采集天气网实时信息

      相信小伙伴们都知道今冬以来范围最广.持续时间最长.影响最重的一场低温雨雪冰冻天气过程正在进行中.预计,今天安徽.江苏.浙江.湖北.湖南等地有暴雪,局地大暴雪,新增积雪深度4-8厘米,局地可达10- ...

  5. 【python爬虫】根据查询词爬取网站返回结果

    最近在做语义方面的问题,需要反义词.就在网上找反义词大全之类的,但是大多不全,没有我想要的.然后就找相关的网站,发现了http://fanyici.xpcha.com/5f7x868lizu.html ...

  6. python爬虫练习 -- 签名器+GUI界面(Tkinter)

    效果图: 实现步骤如下: 实现原理:其实就是套了一层GUI的壳,主要还是爬虫抓取某个网站返回的数据,然后利用python自带的GUI工具包Tkinter来实现gui界面: 1.爬虫分析: 目标站点:h ...

  7. python爬虫——简易天气爬取

    通过爬虫,抓取http://www.weather.com.cn的天气信息 功能——输入城市代码,获取当日天气,简单的beautifulsoup和requests实现.(城市代码可百度查询,不全部展示 ...

  8. Python爬虫-播报天气信息(生成exe文件)待续

    #!/usr/bin/env python3 # -*- coding : utf-8 -*- '''1.从https://my.oschina.net/joanfen/blog/140364获取要播 ...

  9. python爬虫学习之查询IP地址对应的归属地

    话不多说,直接上代码吧. import requests def getIpAddr(url): response = requests.get(url) response.encoding=resp ...

随机推荐

  1. Scala使用备注一

    package com.ws.spark.study.scala import java.io.File import org.scalatest.FlatSpec import scala.io.S ...

  2. flask蓝图blueprint是什么

    蓝图 blueprint 简单的说,就是帮助我们对flask程序进行目录结构的划分:django项目创建时,是自动生成项目目录,而在flask这里,需要我们自己来规划,这就需要blueprint来将整 ...

  3. 双端队列 【deque】

    题目链接:https://ac.nowcoder.com/acm/contest/1071/D 还是第一次简单运用双端队列.顾名思义:队列的头尾都可以进行插入跟删除操作. 存在于头文件 deque 中 ...

  4. JavaSE基础(四)--Java基本数据类型

    Java 基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过定义不 ...

  5. Docker下mysql容器开启binlog日志(保留7天)

    现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...

  6. leetcode 算法整理

    一 字符串中的最大回文串(第5题) Given a string s, find the longest palindromic substring in s. You may assume that ...

  7. Windows32位或64位下载安装配置Scala

    [学习笔记] Windows 32位或64位下载安装配置Scala: 1)下载地址:http://www.scala-lang.org/download/,看我的spark那节,要求scala是2.1 ...

  8. 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...

  9. 2017多校赛 Function

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  10. StoneTab标签页CAD插件 3.2.0

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...