1.完整代码:

import time
import urllib.request #发送网络请求,获取数据
import gzip #压缩和解压缩模块
import json #解析获得的数据
from tkinter import * root1 = Tk() #用tkinter建立根窗口
root1.title('天气查询xgj@V1.0')#窗口标题
root1.geometry('1300x800+500+0') #注意x=是小写的字母x,不是乘号
root1.configure(bg='black') #构建一个函数,bg=背景颜色设置
Label(root1,text = '请输入要查询天气的城市:',font=10,bg='black',fg='purple').grid(row=0,column=0)#设置标签并调整位置
enter = Entry(root1,font=10,fg='purple')#输入框设置,字体大小
enter.grid(row = 0,column=1,padx = 20, pady = 20)#调整位置
enter.insert(0,'北京')#设置默认文本,初始为北京 def get_weather_data() :#爬虫部分:获取网站数据
city_name = enter.get()#获取输入框的内容
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
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("提示","你输入的城市名有误,或者天气中心未收录你所在城市"))
else:
show_data(weather_dict,city_name) #如果正确,那就进入展示数据窗口和内容 #定义展示数据窗口和内容设置
def show_data(weather_dict,city_name):#显示数据窗口的设置
forecast = weather_dict.get('data').get('forecast')#获取数据块
root2=Tk()#弹出天气查询结果的窗口,第2个窗口
root2.geometry('1500x500+500+0')#修改窗口大小
root2.configure(bg='black')
root2.title(city_name + '的未来5天的天气状况')#副窗口标题 #设置日期列表
for i in range(5):#将每一天的数据放入列表中 ,列表中5个数据
LANGS = [(forecast[i].get('date'),'日期'),
(forecast[i].get('fengxiang'),'风向'),
(forecast[i].get('high'),'最高温'),
(forecast[i].get('low'),'最低温'),
(forecast[i].get('type'),'天气')]
group = LabelFrame(root2,text = '天气状况',font=6,bg='black',fg='purple',padx = 0,pady = 0)#框架
group.pack(padx=11,pady=0,side = LEFT)#放置框架
for lang, value in LANGS:#将数据放入框架中
c = Label(group,text = value + ': ' + lang,fg='green',font=8,bg='black')
c.pack(anchor = W)
#第2个窗口root2的标签和按钮设置
Label(root2,font=10,text = '今日:' + weather_dict.get('data').get('ganmao'),
fg = 'red',bg='black').place(x=40,y=20,height=40)#温馨提示
Label(root2,text = "↑这是今天的天气",font=8,fg = "red",bg = "black").place(x=10,y=450,width=300,height=50)#作者网站
Button(root2,text = '确认并退出',font=10,fg = "purple",bg = "black",command = root2.quit).place(x=1200,y=450,width = 200,height=50)#退出按钮
root2.mainloop()
#主界面:第1个窗口的按钮设置布置按键
Button(root1, text = "确认",width=10,font=10,bg='black',fg='purple',command = get_weather_data)\
.grid(row = 3, column=0,sticky = W, padx = 10, pady = 5)
Button(root1, text = '退出',width=10,font=10,bg='black',fg='purple',command = root1.quit)\
.grid(row = 3, column = 1, sticky = E, padx = 10, pady = 5) # 配置,放在这里,那么主窗口的2个按钮就先显示出来
# 要打开的图像
image1 = "open.gif" #注意tkinter中需要gif格式的图片,默认目录下
image2 = "welcome.gif" #也可以单独设置目录或者路径 #python3的特性,一行依次赋值
x0, y0=50.0,50.0 # 初始坐标
x,y=[x0],[y0] # 列表将包含所有的x和y坐标.到目前为止,他们只包含初始坐标
vx,vy=1.0,0.5 # 每次移动的速度或距离
# 边界,这里要考虑到图片的大小,要预留一半的长和宽
x_min,y_min,x_max,y_max=46.0,46.0,754.0,554.0
range_min,range_max=1,2000 # 运行步数 # 创建500次的x和y坐标
for t in range(range_min, range_max):
# 新坐标等于旧坐标加每次移动的距离
new_x = x[t - 1] + vx
new_y = y[t - 1] + vy
# 如果已经越过边界,反转方向
if new_x >= x_max or new_x <= x_min:
vx = vx * -1.0
if new_y >= y_max or new_y <= y_min:
vy = vy * -1.0
# 添加新的值到列表
x.append(new_x)
y.append(new_y) canvas = Canvas(root1,width=800, height=600, bg='white')
canvas.grid(row = 2,column=0)#调整位置 photo1 = PhotoImage(file=image1)
photo2 = PhotoImage(file=image2) #add width1 = photo1.width()
height1 = photo1.height()
image_x = (width1) / 2.0
image_y = (height1) / 2.0 # 每次的移动
for t in range(range_min, range_max):
canvas.create_image(x[t], y[t], image=photo1, tag="pic") #tag是这张图片的标签,这里需要
canvas.update()
# 暂停多少秒,然后删除图像
time.sleep(0.001) #1秒,缓慢;如果0.025s等于每秒40帧;0.001s很快
canvas.delete("pic") #因为这里需要删除它 #等上面动画结束后,再出现这个图片
canvas.create_image(400, 300, image=photo2) #这里不需要tag
canvas.update() #主窗口循环走起
root1.mainloop()

2. 图片可自定义:注意是gif格式。

python+tkinter+动画图片+爬虫(查询天气)的GUI图形界面设计的更多相关文章

  1. python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值

    Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...

  2. Tkinter图形界面设计(GUI)

    [因为这是我第一个接触的GUI图形界面python库,现在也不用了,所以大多数内容都来自之前花 钱买的一些快速入门的内容,可以当作简单的知识点查询使用] 在此声明:内容来自微信公众号GitChat,付 ...

  3. 简单的爬虫程序以及使用PYQT进行界面设计(包含源码解析)

    由于这个是毕业设计的内容,而且还是跨专业的.爬虫程序肯定是很简单的,就是调用Yahoo的API进行爬取图片.这篇博客主要讲的是基础的界面设计. 放上源码,然后分部解析一下重要的地方.注:flickra ...

  4. PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法大全及对比分析

    一. 概述 PyQt的图形界面应用中,事件处理类似于Windows系统的消息处理.一个带图形界面的应用程序启动后,事件处理就是应用的主循环,事件处理负责接收事件.分发事件.接收应用处理事件的返回结果, ...

  5. python 斗图图片爬虫

    捣鼓了三小时,有一些小Bug,望大佬指导 废话不说,直接上代码: #!/usr/bin/python3 # -*- coding:UTF-8 -*- import os,re,requests fro ...

  6. Python tkinter库将matplotlib图表显示在GUI窗口上,并实时更新刷新数据

    代码 1 ''' 2 使用matplotlib创建图表,并显示在tk窗口 3 ''' 4 import matplotlib.pyplot as plt 5 from matplotlib.pylab ...

  7. 【Python】:简单爬虫作业

    使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...

  8. python Tkinter 编程

    Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序. 由于 Tkinter 是内置到 python 的安装包中.只要安装好 Py ...

  9. PyQt(Python+Qt)学习随笔:print标准输出sys.stdout以及stderr重定向QTextBrowser等图形界面对象

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 <在Python实现print标准输出sys.stdout.st ...

随机推荐

  1. Struts2-057远程代码执行漏洞(s2-057/CVE-2018-11776)复现

    参考了大佬的链接:https://github.com/jas502n/St2-057 00x01前言 Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用 ...

  2. [Python]jieba切词 添加字典 去除停用词、单字 python 2020.2.10

    源码如下: import jieba import io import re #jieba.load_userdict("E:/xinxi2.txt") patton=re.com ...

  3. ALSA lib编译

    http://blog.sina.com.cn/s/blog_7d7e9d0f0101lqlp.html alsa  lib: #!bin/sh rm -rf ./output/* mkdir -p ...

  4. 解决myeclipse2017ci7破解后闪退问题

    解决myeclipse2017ci7破解后闪退问题 打开myeclipse.ini修改为: #utf8 (do not remove)-startupplugins/org.eclipse.equin ...

  5. PP: Imaging time-series to improve classification and imputation

    From: University of Maryland encode time series as different types of images. reformulate features o ...

  6. gz、tar、zip、bz2压缩和解压缩命令

    gzip 压缩后的格式为:*.gz 这种压缩方式不能保存原文件:且不能压缩目录 命令举例:#压缩[root@localhost tmp]# gzip buodo[root@localhost tmp] ...

  7. resize函数

    #include<opencv2/opencv.hpp>#include<opencv2/highgui.hpp> using namespace cv;using names ...

  8. PHP毫秒

    PHP毫秒   php的毫秒是没有默认函数的,但提供了一个microtime()函数,该函数返回包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可以很容易定义一个返回毫秒数的函数,例如: ...

  9. ReLU(inplace=True),这里的inplace=true的意思

    ReLU(inplace=True),这里的inplace=true的意思 待办 inplace=True means that it will modify the input directly, ...

  10. vector,list不是模板

    vector和list在命名空间std里,还需要添加声明 using namespace std;   或者 std::list 也可以.